Let's learn java programming language with easy steps

Wednesday, 15 November 2017

How to get Current Date and Time with Java 8 New Features


Current Date and Time in Java 8

Current Date and Time in java 8 Features

In this article, We are going to learn how to get current data and time by using java 8 new features with simple examples step-by-step.

Here we will use some new java 8 date time API to display the data and time in java.

Here we are going to use some java 8 date and time classes which are given below.

  • java.time.LocalDate class
  • java.time.LocalTime class
  • java.time.LocalDateTime class
We can also use some old classes to get the current data and time in java e.g Date and Calendar classes but here we will focus only above listed 3 classes which is java 8 LocalDate, LocalTime, and LocalDateTime.

Let's start with LocalDate class.


Java 8 LocalDate Class

Java 8 LocalDate class are defined in java.time.* package and it contains several useful methods but here we are using now() method which is a static method.

LocalDate class represents date in this format year-month-day

Java 8 LocalDate class is a final class i.e it is immutable class and LocalDate class extends Object class and implements several interfaces like Serializable, ChronoLocalDate, Temporal, TemporalAdjuster.


LocalDate Class Example 

This is simple LocalDate class example for printing date.

class DateExample
{
public static void main(String args[])
{
//display the current date from the system clock in the default time zone
System.out.println("Current date is : "+java.time.LocalDate.now());
}
}

Output: Current date is : 2017-11-16


Java 8 LocalTime Class

Java 8 LocalTime class is defined in java.time.* package and it contains several useful methods but here we are going to use LocalTime.now() method which is static method.

LocalTime class represents time in this format hour-minute-second.

Time is represented to nanosecond precision.

Java 8 LocalTime class is also a immutable class i.e it is final class and it extends Object class and implement several interface like Serializable, Comparable<LocalTime>, etc.

LocalTime Class Example

This is simple LocalTime class example for printing time in java.

class TimeExample
{
public static void main(String args[])
{
//display the current time form the system clock in the default time-zone
System.out.println("Current time is : "+java.time.LocalTime.now());
}
}

Output: Current time is : 13:9:59.546


Java 8 LocalDateTime Class

Java 8 LocalDateTime class is also defined in java.time.* package and it also contains some useful method but here we will use LocalDateTime.now() method for representing date and time in java.

LocalDateTime class represents date and time both int this format year-month-day-hour-minute-second.

LocalDateTime class is final class i.e it immutable class and this class extends Object class and implement interfaces like Serializable, Temporal, TemporalAdjuster, etc.

LocalDateTime Class Example

This is simple example LocalDateTime class where will display both date and time together.

class DateTimeExample
{
public static void main(String args[])
{
//display the date and time from the system clock in default time-zone
System.out.println("Current date-time is : "+java.time.now());
}
}

Output: Current date-time is : 2017-11-16T13:27:10.087

here we learned how to get current date and time by using some new java 8 API like LocalDate class, LocalTime class and LocalDateTime class. There are other new classes in java 8 date and time api but we will learn in later.






Share:

Tuesday, 14 November 2017

What is Recursion in C Language with Example


Recursion in C

Recursion in  C Language

When function calling itself is called recursion in c programming language. In other words, When function is call in the same function is known as recursion in c language.

The function which calls the same function is called recursive function in C.

Recursive functions is quite useful in any mathematical calculation for solving a problem e.g useful in calculate factorial of any number, perform fibonacci series, etc.

After explaining recursion in c, Let's see the syntax of recursion in c.

Syntax:

function()
{
function();//calling itself, known as recursion or recursive function
}

Let's talk about some advantages and disadvantages of recursion in c language.


Advantages of Recursion in C

There are some advantages of using recursion in c language.
  • Recursion will be useful when same kind of work has to be continued for a finite no input or time.
  • We can reduce the length of the code by using recursion in c.
  • Recursion is very useful for solving data structure problems.

Disadvantages of Recursion in C

There are some disadvantages of recursion in c language.
  • It requires more memory because recursive calls along with automatic variables are stored on the stack.
  • Recursive solution is always logical and it is very difficult to trace i.e debug and analyse.

Now let's explain recursion in c with examples.

Factorial using recursion in c

This is the simple factorial program with recursion in c language.

#include<stdio.h>
#include<conio.h>
int fact(int);

void main()
{
int number, a;
clrscr();
printf("Enter number");
scanf("%d", &number);

a = fact(number);
printf("Factorial is %d\n", a);
getch();
}

int fact(int x)
{
int f;
if(x ==1)
return 1;
else
f = x*fact(x - 1);//recursion performed
return f;
}
}

Output: Enter number 5
             Factorial is 120


Fibonacci Series Program in C with Recursion

This is simple fibonacci series program in c by using recursion function.

#include<stdio.h>
int fibonacci(int);

main()
{
int n, c, i = 0;
scanf("%d", &n);
printf("febonacci series \n");

for(c = 1; c <= n; c++)
{
printf("%d\n", febonacci(i));
i++;
}

return 0;
}

int febonacci(int n)
{
if(n == 0)
return 0;
else if(n == 1)
return 1;
else 
return (febonacci(n-1) + febonacci(n-2));
}

Here we discussed what is recursion in c language with examples and their advantages and disadvantages.

Check other C tutorial articles e.g data types in c language and printf() and scanf() function in c.





Share:

How to Write Comments in C Programming


Comments in C Programming Language

Comments in C Language

We are going to discuss, How to write comments in c programming language with step-by-step with simple examples. Let's start...


What is the purpose of comments in program?

Basically comments are used for description of every line of code which is written so that the programmer and other user can understand the meaning of each line of code in program.

Comments are mostly used for documenting of code.

The particular area of comments where we used are totally ignored by the compiler and its output does not print on the console.


Types of Comments in C Language

There are two types of comments in c language and these are...

  1. Single Line Comments
  2. Multiple Line Comments

Single Line Comments in C Language

We can use double slash ( // ) for representing single line comments in c language.

Syntax:

//this is my first single line comment in c program

Single Line Comments Example

This is single line comments in c program with output.

#include<stdio.h>
#include<conio.h>
void main()
{
//Variables declaration for addition
int a = 40;
int b = 40;
int c;
clrscr();

printf("value of a : %d\n", a);
printf("value of b : %d\n", b);

c = a+b;
printf("value of c : %d", c);
getch();
}

Output: value of a : 40
             value of b : 40
             value of c : 80


Another Example of Single Line Comments in C

This is another single line comment example where we will use more than one single line comments. You can use any number of single line comments at any place.

//single line comments in c
#include<stdio.h>
#include<conio.h>
void main()//main method
{
int a = 30;//variable declaration
clrscr();
printf("value of a : %d", a);
getch();
}

Output: value of a : 30


Multiple Line Comments in C Language

We can use slash asterisk (/*......*/) for representing multiple line comments in c language.

Syntax:

/* This is my 
      first multiple line comments
      in c language. */

            or

/* multiple line comments */

Multiple Lines Comments Example

This is multiple line comments example in c language.

#include<stdio.h>
#include<conio.h>
void main()/* main function */
{
/* variable declaration 
     a variable
     b variable 
     c variable */
int a = 40;
int b = 20;
int c;
clrscr();

c = a+b;
printf("addition of two number : %d ", c);
getch();
}

Output: addition of two number : 60

We cannot define comments into another comments i.e nested comments are not allowed in c.

For example:

#include<stdio.h>
#include<conio.h>
void main()
{
/* 
   /* nested comments not allowed */

*/
printf("nested not allowed");
getch();
}

Output: compile time error

Here we discussed, What is comments in c and the advantages of using comments in programming and how to write comments in c programming.

Learn how to run first program in c







Share:

Sunday, 12 November 2017

How to Use Pointers in C


Pointer in C Programming

In this article, We are going to learn what is a pointer and how to use pointers in c language with simple examples so that you can easily understand the pointer concept in c programming language.

Let's start with the definition of the pointer in c programming language and then take a lot of examples of c pointer to understand better.


What is Pointer in C Language?

Pointer is a special type of variable which stores or hold the address of another variable. Every variable has a particular address in a memory. We can know the address of variable and pointer easily by using some specifiers.


Advantages of Pointers in C

There are some advantages of pointer concept in c language.
  • Pointer reduces the length of the code and improves the performance because it can access address of a variable directly.
  • Pointer increases the processing speed.
  • Pointer saves memory.
  • Using pointer, We can return multiple values from any function.
  • Using pointer, You can access any memory location.

Symbols in C Pointer

There are some symbols or operator which is used in c pointer concept.

(1) Symbol - &(ampersand sign)

The name of this symbol is "address of operator" which is used to display the address of a variable.

(2) Symbol - *(asterisk sign)

The name of this symbol "indirection operator" which accesses the value at the address.


Address of Operator - &(ampersand sign)

In this example, We will print the address of variable 'x' by using the address of operator i.e & and there we will use %u format specifier for displaying the address of a variable.

For example:

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 50;
clrscr();
printf("Value of variable x is : %d\n ", x);//print value of x i.e 50
printf("Address of variable x is : %u ", &x);//print address of variable x
getch();
}

Output: Value of variable x is : 50
             Address of variable x is : 65524

                                                                 OR

You can also use %p format specifier for display variable's address in hex format.

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 50;
clrscr();
printf("The value of variable x is : %d\n ", x);//print 50
printf("Address of variable x is : %p", &x);//print address in hex format
getch();
}

When you will convert 65524 (decimal) to hex number, it will give you FFF4 in result. 

Now we are able to know the address of any variable and now it is time to know how to hold or store these variable's address into another variable, for this we will use the pointer in c programming language.


How to declare pointers in C?

By using *(asterisk sign) we can declare pointers in c language.

For example:

int *a;//pointer to integer type

char *b;//pointer to character type

double *c;//pointer to double type

float *d;//pointer to float type

Let's understand pointer in c language with example.


Example of Pointer in C

In this example, We will store the address of a variable 'x' into pointer variable '*p" and then print the address and value of a variable by using pointer variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 400;//variable x
int *p;//pointer variable
clrscr();
p = &x;//store the address of x variable 

printf("Value of variable x : %d\n ", x);//print 400
printf("Address of variable x : %p\n ", &x);//print address of x
printf("Address of variable p : %p\n ", p);//print address of p
printf("Value of variable p : %d ", *p);//print value of p

getch();

}
}

Output: Value of variable x : 400
             Address of variable x : FFF4
             Address of variable p : FFF4
             Value of variable p : 400

Note: The data type of both variable(x) and pointer(*p) variable must be same. 

For example: 

int x = 50;//integer data type 
int *p;//integer data type


Another example of Pointer in C

Pointer in C programming

In the above image, Here 'a' is a simple variable with value 40 and with memory address fff4 and 'p' is a pointer variable which stores and holds the address of variable 'a' i.e fff4. Here fff2 is the memory address of pointer variable.

By using the address of a variable 'a' into pointer p we can print the address and value of variable 'a'.

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 40;
int *p;
clrscr();
p = &a;
printf("value of a : %d\n ", a);
printf("address of a : %x\n", &a);
printf("address of p : %x\n", p);
printf("address of p : %x\n", &p);
printf("value of p : %d ", *p);

getch();
}
}

Output: value of a : 40
             address of a : fff4
             address of p : fff4
             address of p : fff2
             value of p : 40

You can use %x format specifier for display address of the variable.


Null Pointer in C

A pointer that is not assigned any value but NULL is known as a Null pointer in c programming language and this done at the time of variable declaration.

For example:

int *p = NULL;


In this article, We discussed what is pointer in c programming language and how to use pointers in c and what is null pointers with simple and easy examples. 

Share:

Thursday, 9 November 2017

Java 8 Lambda Expressions Tutorial with Examples


Java 8 Lambda Expressions Tutorial

Java 8 lambda Expression Tutorial

In this article, We are going to cover java 8 lambda expressions tutorial with examples one-by-one in a simple way. Lambda expression is a new feature which introduced in java 8 programming language.


What is Lambda Expression?

There are many new features introduced in java 8 e.g default and static method in an interface, Array parallel sorting, StringJoiner class, etc. and Lambda expression is one of them which is introduced in java 8 version.

Java 8 lambda expression is used to provide the implementation of the functional interface, An interface which has only one abstract method known as a functional interface.

Lambda expression is anonymous function i.e function with no name. This function does not belong to any class.

Lambda expression concept was first introduced in LISP programming language and the in other programming languages like C,C++,Ruby,Scala,.Net, C#, Java, etc.

Lambda expression is also useful for iterate and extract the data from the collection.


Advantages of Lambda Expression

There are many advantages of lambda expressions and these are given below.
  • To enable functional programming in java.
  • Lambda expression reduces the length of the code.
  • By using lambda expression we write more readable and maintainable code in java.
  • To enable parallel processing.

Syntax of Lambda Expression

(parameter_list) -> {body}

It contains three parts:

Parameter List: Parameter list may be empty or non-empty.

Arrow operator: This is lambda operator which connect the parameter list and body expression.

Body: It contains expression and statements for lambda expressions.


Java Lambda Expression Example

In this example, we will provide the implementation of function interface by using the lambda expression.

For example:

@FunctionalInterface
interface LambdaExample
{
public void eat();
}
class Test 
{
public static void main(String args[])
{
LambdaExample l = () -> {

System.out.println("eat rice");

};
l.eat();
}
}

Output: eat rice


Java Lambda Expression Example with No Parameter

This is the first java 8 anonymous function example i.e lambda expression example with no parameter.

For example:

@FunctionalInterface
interface MyFunctional
{
public String run();//without parameter
}
class First 
{
public static void main(String args[])
{
MyFunctional mf = () -> {

return "fast";
};

System.out.println(mf.run());
}
}

Output: fast

@FunctionInterface is used to declare an interface as the functional interface in java. It is optional.


Java Lambda Expression Example with One Parameter

This single parameter example of java 8 lambda expression.

For example:

interface My
{
public String watch(String s);
}
class Second 
{
public static void main(String args[])
{
My m = (watching) -> {

return "enjoying "+watching;
};
System.out.println(m.watch("cricket"));
}
}

Output: enjoying cricket


Java Lambda Expression Example with multiple parameters

In this lambda expression example, we will pass two parameters.

For example:

interface My
{
public String hello(String a, String b, String c);
}
class Third 
{
public static void main(String args[])
{
My m = (a, b, c) -> a+b+c;
System.out.println(m.hello("I", "am", "good"));
}
}

Output: I am good


Java Lambda Expression Example with Collection

This is the example of lambda expression with collection and foreach loop.

For example:

import java.util.*;
class LambdaWithCollection
{
public static void main(String args[])
{
List<String> list = new ArrayList<String>();
list.add("red");
list.add("black");
list.add("pink");
list.add("white");
list.add("orange");

list.forEach((n) -> System.out.println(n));
}
}

Output: red
             black
             pink
             white
             orange

Here we have discussed java 8 lambda expression with simple and easy examples.


Share:

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Translate