Wednesday, July 1, 2020

part-3

Type Casting:
Process of converting a particular data type into required data type is called as type casting.
1. Auto Up casting:
a. converting smaller data type to bigger data type is called as auto up casting
b. During up casting if data loss happens then we would get an error

Example 1:
public class A {
public static void main(String args[]) {
int i = 10;
long j = i;

System.out.println(j);
}

}
Output:
10

Example 2:
public class A {
public static void main(String args[]) {
long i = 10; //8 bytes
int j = i;

System.out.println(j);
}
}
Output:
Error because copying data from bigger memory to smaller memory is not an automated process

Example 3:
public class A {
public static void main(String args[]) {
float i = 10.3f; //4 bytes
double j = i;

System.out.println(j);
}
}
Output:
10.300000190734863

Example 4:
public class A {

public static void main(String args[]) {
float i = 10.3f; //4 bytes
long j = i;

System.out.println(j);
}
}
Output:
Error because during data copying .3 decimal value will be lost

2. Explicit Down casting:
a. Converting bigger data type to smaller data type is called as explicit down casting
b. During explicit down casting data loss might happen

Example 1:
public class A {
public static void main(String args[]) {
int i = 10;
byte j = (byte)i;
System.out.println(j);
}
}

Example 2:
public class A {
public static void main(String args[]) {
long i = 10;
int j = (int)i;
System.out.println(j);
}

}
Output:
10

Example 3:
public class A {
public static void main(String args[]) {
double i = 10.3;
float j = (float)i;
System.out.println(j);
}
}
Output:
10.3

Example 4:
public class A {
public static void main(String args[]) {
float i = 10.3f;
int j = (int)i;
System.out.println(j);
}
}
Output:
10, in this case .3 value is lost and only integer part is copied in variable j

Example 5:
public class A {
public static void main(String args[]) {
float i = 10.3f;

long j = (long)i;
System.out.println(j);
}
}
Output:
10

Example 6:
public class A {
public static void main(String args[]) {
long i = 9632882052L;
int j = (int)i;
System.out.println(j);
}
}
Output:
1042947460

Extra examples on auto conversion:

Example 1:
public class A {
public static void main(String args[]) {
int i = 'b';
System.out.println(i);
}
}
Output: 98

Example 2:

public class A {
public static void main(String args[]) {
int i = '39';
System.out.println(i);
}
}
Output:
2319

Example 3:
public class A {
public static void main(String args[]) {
int i =a;
System.out.println(i);
}
}
Output:
97

PART-2

Example 2:
public class A {
public static void main(String args[]) {
int i = 10;
int j = ++i + ++i + ++i;

System.out.println(i);
System.out.println(j);
}
}
Output:
13
36

Example 3:
public class A {
public static void main(String args[]) {
int i = 10;
int j = ++i + i + ++i;

System.out.println(i);
System.out.println(j);
}
}
Output:
12
34

Examples on post and pre increment together
Example 1:
public class A {
public static void main(String args[]) {
int i = 10;
int j = ++i + i++;

System.out.println(i);
System.out.println(j);
}
}
Output:
12
22

Example 2:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i++ + ++i + i++ + i++;

System.out.println(i);
System.out.println(j);
}
}
Output:
14
47

Example 3:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i-- + i--;

System.out.println(i);
System.out.println(j);
}
}
Output:
8
19

Example 4:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i++ + i-- + --i + ++i;

System.out.println(i);
System.out.println(j);
}
}
Output:
10
40

Example 5:

public class A {
public static void main(String args[]) {
int i = 10;
System.out.println(i++);
System.out.println(++i);
System.out.println(--i);
System.out.println(i--);
System.out.println(i);

}
}
Output:
10
12
11
11
10

Example 6:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i++ - ++i;

System.out.println(i);
System.out.println(j);
}
}

Output:
12
-2




Data types in Java in Type casting
 Data typeMemory Size  Default Value
 byte 1 byte0 
 short2 bytes  0
int 4 bytes 0 
 long8 bytes 0 
 float4 bytes 0.0 
double 8 bytes 0.0 
boolean NA false 
var(JDK 1.10) NA  NA
 char2 bytes  Empty space 
String(Class) Is Class Not a datatype  null


  

Which data type I should use to store 10 digit mobile phone number?

Example 1:
public class A {

public static void main(String args[]) {
long mobilenumber = 9632882052L;
System.out.println(mobilenumber);
}

}
Output:
9632882052

Example :
public class A {

public static void main(String args[]) {
float i = 10.3F;
System.out.println(i);
}
}
Output:
10.3

var datatype in java ?
a. var data type can store any kind of value in it
b. var data type cannot be static variable
c. var data type cannot be non static variable
Example 1:
public class A {
public static void main(String args[]) {

var i = true;
var j = "P";
var k = true;
var z = 10.3;

System.out.println(i);
System.out.println(j);
System.out.println(k);
System.out.println(z);
}
}
Output:

true
P
true
10.3


Example 2:
public class A {
static var i = 10;
public static void main(String args[]) {

}
}
Output:
A.java:2: error: 'var'; is not allowed here
static var i = 10;
^
1 error


Example 3:
public class A {
var i = 10;
public static void main(String args[]) {

}
}
Output:

A.java:2: error:'var'  is not allowed here
var i = 10;

^
1 error

Note: Printing multiple values in java single print statement:

Example:
public class A {

public static void main(String args[]) {
int i = 10;
int j = 100;
System.out.println(i+"  ";+j);
}
}
Output:
10 100

PART-1

What is a class ?
a. Class helps us to generate objects
b. Class will generate object whenever we make a request to the class using new keyword

What is new keyword ?
a. It send request to the class to create object
b. Once the class has created an object. new keyword gets the address of the object and stores
that in a reference variable

Syntax to create a class
class Name {

}

Syntax to create object:
className variableName = new className();

Example 1:
public class A {
public static void main(String[] args){
A a1 = new A();
System.out.println(a1);

A a2 = new A();
System.out.println(a2);

A a3 = new A();

System.out.println(a3);
}
}

Output:
A@7960847b
A@6a6824be
A@5c8da962

Static keyword versus non static in java ?
Non static members
a. These members are loaded into the object whenever an object is created
b. Without creating an object non static member cannot be accessed

Example 1:
public class A {
int i = 10; //non static
public static void main(String[] args){
A a1 = new A();
System.out.println(a1.i);
}
}

Output:
10

Example 2:
public class A {
int i = 10; //non static
int j = 100;//non static
public static void main(String[] args){
A a1 = new A();

System.out.println(a1.i);
System.out.println(a1.j);
}
}
Output:
10
100

Static members:
a. These members belongs to the class. To access these members do not create object
b. These members are loaded into the common memory of the class only once

Example 1:
public class A {
static int i = 10; //static
static int j = 100;//static
public static void main(String[] args){
System.out.println(A.i);
System.out.println(A.j);
}
}
Output:
10
100

Example 2:
public class A {
int i = 10;//belongs to object
static int j = 100; //static (Belongs to class)

public static void main(String[] args){
A a1 = new A();
System.out.println(a1.i);
System.out.println(A.j);
}
}

Output:
10
100

Example 3:
public class A {
int i = 10;//belongs to object
public static void main(String[] args){
A a1 = new A();
a1.i = 100;
System.out.println(a1.i);

A a2 = new A();
System.out.println(a2.i);
}
}

Output:
100
10

Overview of methods:
1. Methods will execute only when we call it

Example 1:
public class A {
public static void main(String args[]) {
A a1 = new A();
a1.test();
}
public void test(){
System.out.println(500);
}
}
Output:
500

Example 2:
public class A {
public static void main(String args[]) {
A.test();
}
public static void test(){
System.out.println(500);
}
}

Output:
500


Example 3:

public class A {
public static void main(String args[]) {

A a1 = new A();
a1.test();

}

public void test(){//non static--class
System.out.println(100);
}
}

Output:
100


Example 4:
public class A {
public static void main(String args[]) {

A.test();

}

public static void test(){//static--class
System.out.println(100);
}
}

Output:

100

Types Of Variables in Java
There are four types of variables in java
1. Local variables
2. static variables
3. non-static variables
4. reference variables

Local Variables:

a. Local variables are created inside a method
b. Local variables can be used / accessed only within created method
c. Local variables are accessed directly with its name
d. Local variables cannot be used without initializing

Example 1:
public class A {
public static void main(String args[]) {
A a1 = new A();
a1.test();

}
public void test(){
int i = 10;//Created
System.out.println(i);//Using
}
}

Example 2:

public class A {
public static void main(String args[]) {
System.out.println(i);
A a1 = new A();
a1.test();

}

public void test(){
int i = 10;//Created
System.out.println(i);//Using
}
}

Output:
Error because we are using variable i outside the test() method

Example 3:
public class A {
public static void main(String args[]) {
int i = 10;//created
System.out.println(i);//using
A a1 = new A();
a1.test();

}

public void test(){

System.out.println(i);//using outside main
}
}

Output:
Error
Example 4:
public class A {
public static void main(String args[]) {
int i = 10;//created
System.out.println(i);//using
}
}

Output:
10

Example 5:
public class A {
public static void main(String args[]) {
int i;//created
System.out.println(i);//using
}
}

Output:
/A.java:4: error: variable i might not have been initialized
System.out.println(i);//using
^
1 error

Static variables

a. Static variables are created outside all the methods but inside a class using static keyword
b. These variables can be used anywhere in the program
c. Static variables can be accessed in three ways
1. ClassName.variableName
2. variableName
3. referenceVariable.memberName
4. It is not mandatory to initialize static variables, if we do not initialize then depending on the
data type auto initialization would happen

Example 1:
public class A {
//here static variables are created
static int i = 10;

public static void main(String args[]) {
//Here local variables are created
System.out.println(A.i);
A a1 = new A();
a1.test();
}

public void test(){
System.out.println(A.i);
}
}
Output:
10
10

Example 2:

public class A {
public static void main(String args[]) {
static int i = 10;
}

}

Output:
Error because static variables cannot be created inside a method

Example 3:
public class A {
static int i = 10;
public static void main(String args[]) {
System.out.println(A.i);
System.out.println(i);

A a1 = new A();
System.out.println(a1.i);//A.i
}

}

Output:
10
10
10

Example 4:
public class A {
static int i;//0

public static void main(String args[]) {
System.out.println(A.i);

}

}

Output:
0

Non Static variables"
a. Non static variables created outside all the methods but inside a class without static keyword
b. These variables belongs to the object and only after object creation these variables can be
accessed
c. It is not mandatory to initialize non static variables, if we do not initialize then depending on the
data type auto initialization would happen

Example 1:
public class A {
//static variable is created here
//nonstatic varoable is also created here
int i = 10;
public static void main(String args[]) {
//Local variable is created here
A a1 = new A();
System.out.println(a1.i);
}

}

Output:
10

Example 2:
public class A {
//static variable is created here
//nonstatic varoable is also created here
int i ;
public static void main(String args[]) {
//Local variable is created here
A a1 = new A();
System.out.println(a1.i);
}

}

Output:
0

Reference Variables:
a. The main purpose of reference variables is to store objects address
b. Data type for a reference variable is class name in java
Reference variables in java is categorized as

1. Local reference Variable:
a. These variables are created inside a method and should be used only within created method
b. Local reference variables if used without initializing it then we would get an error

Example 1:
public class A {
public static void main(String args[]) {
A a1 = new A();
System.out.println(a1);
}

public void test(){
System.out.println(a1);
}
}

Output:
Error, Because a1 is created in main method and should be used only within main method

Example 2:
public class A {
public static void main(String args[]) {
A a1 ;
System.out.println(a1);
}

}

Output:
Error

2. static reference variables:
a. These variables are created outside all the methods, but inside a class using static keyword.
b. These variables can accessed anywhere in the program
c. If we do not initialize static reference variables then automatically null value will be stored in it
Example 1:
public class A {

static A a1 = new A();//Global access

public static void main(String args[]) {

System.out.println(a1);
a1.test();
}

public void test(){
System.out.println(a1);
}

}
Output:
A@7960847b
A@7960847b
Example 2:
public class A {

static A a1;

public static void main(String args[]) {
System.out.println(a1);

}

}
Output:
null

Unary Operators in Java
1. ++
2. --
a. POST INCREMENT:
Here we increment the value of the variable by one when next time in the
program we see the same variable again
Example 1:
public class A {

public static void main(String args[]) {
int i = 10;
int j = i++ + i++;

System.out.println(i);
System.out.println(j);

}

}

Output:
12
21

Example 2:
public class A {

public static void main(String args[]) {
int i = 10;
int j = i++ + i++ + i++;

System.out.println(i);
System.out.println(j);

}

}

Output:
13
33

Example 3:
public class A {

public static void main(String args[]) {
int i = 10;
int j = i++ ;

System.out.println(i);
System.out.println(j);

}

}

Output:
11
10

Example 4:
public class A {

public static void main(String args[]) {
int i = 10;
int j = i++ ;

System.out.println(i);
System.out.println(j);

}

}
//9632882052

Output:
12
32

b. PRE INCREMENT: 
Here we increment the value of the variable in same step

Example 1:
public class A {

public static void main(String args[]) {
int i = 10;
int j = ++i + ++i ;

System.out.println(i);
System.out.println(j);

}

}
Output:
12
23

Example 2:
public class A {

public static void main(String args[]) {
int i = 10;
int j = ++i + ++i + ++i;

System.out.println(i);
System.out.println(j);

}

}

Output:
13
36

Example 3:
public class A {

public static void main(String args[]) {

int i = 10;
int j = i++ + ++i + i++;

System.out.println(i);
System.out.println(j);

}

}
Output:
13
34

Example 4:

c. POST DECREMENT:
here we decrement the value of the variable by one next time when we see the same variable
Example 1:
public class A {

public static void main(String args[]) {
int i = 10;
int j = i-- + i--;

System.out.println(i);
System.out.println(j);

}

}
//9632882052
Output:
8
19

d. PRE DECREMENT: 
Here we decrement the value of the variable in same step.
Example 1:
public class A {

public static void main(String args[]) {
int i = 10;
int j = --i + --i;

System.out.println(i);
System.out.println(j);

}

}
Output:
8
17

part-3

Type Casting: Process of converting a particular data type into required data type is called as type casting. 1. Auto Up casting: a. convert...