Sie sind auf Seite 1von 11

Data types

Data types specifies the size and type of values that can be stored in an identifier.
Types
There are two types of Datatypes
1. Primitive Datatypes
2. Non-Primitive Datatypes
Primitive Datatypes

1. boolean data type


2. byte data type
3. char data type
4. short data type
5. int data type
6. long data type
7. float data type
8. double data type

Example for Primitive DataTypes:

{
byte age =100;
short boxSeats =123;
int seatsInStadium = 123543;
int calc = -9876345;
long mobileNumber = 9940401212l;
float intrestRate = 12.25f;
double sineVal = 12345.234d;
boolean decision = true;
char ch1 = 88; // code for X
char ch2 = 'Y';
System.out.println("byte Value = "+ age);
System.out.println("short Value = "+ boxSeats);
System.out.println("int Value = "+ seatsInStadium);
System.out.println("int second Value = "+ calc);
System.out.println("long Value = "+ mobileNumber);
System.out.println("float Value = "+ intrestRate);
System.out.println("double Value = "+ sineVal);
System.out.println("boolean Value = "+ decision);
System.out.println("char Value = "+ ch1);
System.out.println("char Value = "+ ch2);
}

Guess?

 What are the data types used for this variable names?
 Sensex, Aadhar, RollNo, NameInitials, Salary and Weather.
 Can we use long data type for age?
 Is valid datatypes helps in increasing performance?
Non-Primitive Data types
1. String
2. Array

1. String
A string is a data type used in programming, it is in a sequence of characters.
We’ll see detailed explanation in further.

2. Array
An array is a group of like-typed variables that are referred to by a common name.

Single Dimension Array


Ex 1:

Code Explanation
{
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)//length is the property of
array
System.out.println(a[i]);
}

Ex 2:
{
int a[]={33,3,4,5};
//printing array
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}

Multi-Dimentional Array

{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}

How to calculate datatype value range?


This formula is used for calculate ranges.
-2^n to 2^n – 1
Note: 1 byte=8bits

For Example:
byte value is 1byte so it has 8 bits.
So that,
-2^7 to 2^7 – 1
-128 to 128 – 1
-128 to 127

 Minimum value is -128 (-2^7)


 Maximum value is 127 (inclusive)(2^7)
Likewise, formula is applicable for all the datatypes.

Data Default Value Default Value of n Range of values that can be stored
Type size
boolean false 1 bit 7 True or false
char '\u0000' 2 byte 15 '\u0000' (or 0) to '\uffff'
byte 0 1 byte 7 -128 to 127
short 0 2 byte 32 -32,768 to 32,767
int 0 4 byte 32 - 2,147,483,648 (-2^31) to
2,147,483,647 (2^31 -1)
long 0L 8 byte 64 -9,223,372,036,854,775,808(-2^63)
to 9,223,372,036,854,775,807(2^63
-1)
float 0.0f 4 byte 32 Unlimited
double 0.0d 8 byte 64 Unlimited

Operators in java

Operators is used to perform operations in java.


Types of Operators

Operator Type Category Precedence

Unary postfix expr++ expr--


prefix ++expr --expr +expr -
expr ~ !
multiplicative */%
Arithmetic additive +-
Shift shift << >> >>>
comparison < > <= >= instanceof
Relational == !=
equality
&
bitwise AND
bitwise exclusive OR ^
Bitwise
bitwise inclusive OR |
logical AND &&
Logical
logical OR ||

Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^=
|= <<= >>= >>>=

Unary Operator
Example 1:

Code Snippet Explanation


{ Body Starts
int x=10; Initializing x=10
System.out.println(x++); //10 (11) Now x=10 is printed and incremented from 10 to 11
System.out.println(++x); //12 11 in a memory and incremented from 11 to 12, x=12 is printed
System.out.println(x--); //12 (11) Since X - - is post decrement, so it is decremented to 12 to 11. X=11 is printed.
System.out.println(--x); //10 - - X is Pre-Decrement so 11 is decremented 10.Finally, X=10 is printed.
} Body Ends

Example 2:

Code Snippet Explanation


{ Body Starts
int a=10; Initializing a=10
int b=10; Initializing b=10
System.out.println(a++ + ++a);//10+12=22 Assigned value a=10 and incremented value is 11(a++) + Again it is incremented to
12(++a) and assigned value is a=12 so that 10+12 in S.O.P
System.out.println(b++ + b++);//10+11=21 Assigned value b=10 and incremented value is 11(b++) + Assigned value b=11 and
} incremented value is 12(b++) so that 10+11 in S.O.P

Body Ends
Explanation:

Example 3:
Code Snippet Explanation
{ Body Starts
int a=10; Initializing a=10
int b=-10; Initializing b=-10
boolean c=true; Initializing c=true
boolean d=false; Initializing d=false
System.out.println(~a);//-11 () minus of total positive value which starts from 0
System.out.println(~b);//9 () positive of total minus, positive starts from 0
System.out.println(!c);//false (opposite of boolean value) ! means opposite of boolean value
System.out.println(!d);//true ! means opposite of boolean value
} Body Ends

Arithmetic Operator

Example 1:
Code Snippet Explanation
{ Body Starts
int a=10; Initializing a=10
int b=5; Initializing b=5
System.out.println(a+b);//15 Substituting a,b vaue in a+b so that 10+5 is 15
System.out.println(a-b);//5 Substituting a,b vaue in a-b so that 10-5 is 10
System.out.println(a*b);//50 Substituting a,b vaue in a*b so that 10*5 is 50
System.out.println(a/b);//2 Substituting a,b vaue in a/b so that 10/5 is 2(Quotient).
System.out.println(a%b);//0 Substituting a,b vaue in a+b so that 10%5 is 0(Reminder).
} Body Ends
Example 2:

Code Snippet Explanation


{ Body Starts
System.out.println(10*10/5+3-1*4/2); 100/5+3-1*2=20+3-2=23-2=21
} Body Ends

Shift Operator
Java Left Shift Operator Example:

Code Snippet Explanation


{ Body Starts
System.out.println(10<<2); 10*2^2=10*4=40
System.out.println(10<<3); 10*2^3=10*8=80
System.out.println(20<<2); 20*2^2=20*4=80
System.out.println(15<<4); 15*2^4=15*16=240
} Body Ends

Java Right Shift Operator


Code Snippet Explanation
{ Body Starts
System.out.println(10>>2); 10/2^2=10/4=2
System.out.println(20>>2); 20/2^2=20/4=5
System.out.println(20>>3); 20/2^3=20/8=2
} Body Ends
Explanation:

Shift Operator Example: >> vs >>>

For positive number, >> and >>> works same

Code Snippet Explanation


{ Body Starts
System.out.println(20>>2); 20/2^2=20/4=5
System.out.println(20>>>2); 20/2^2=20/4=5
//For negative number, >>> changes parity bit (MSB) to 0
System.out.println(-20>>2); -20/2^2=-20/4=-5
System.out.println(-20>>>2);
}
Body Ends

Relational Operator

{ Body Starts
int a = 10; Initializing a=10
int b = 20; Initializing b=20
System.out.println("a == b = " + (a == b) ); Checks whether a value is equal as b value S.O.P will be(False)
System.out.println("a != b = " + (a != b) ); 10 is not equal to 20 S.O.P will be (True)
System.out.println("a > b = " + (a > b) ); 10 is greater than 20 S.O.P will be (False)
System.out.println("a < b = " + (a < b) ); 10 is lesser than 20 S.O.P will be (True)
System.out.println("b >= a = " + (b >= a) ); 20 is greater than or equal to 10 S.O.P will be (True)
System.out.println("b <= a = " + (b <= a) ); 20 is lesser than or equal to 10 S.O.P will be (False)
} Body Ends

Bitwise Operator

Before we go for Bitwise operator lets have little bit idea about that
Note:
Example of Decimal Numbers 34, 67, 89 etc.
Example of Binary Numbers 10111011, 00010011, 00110001 etc.
Do you have idea to convert Decimal to Binary?
Ex 1:
Q. 60
This is the way to calculate Binary digits.

{ Body Starts

int a = 60; /* 60 = 0011 1100 */ Initializing a=10

int b = 13; /* 13 = 0000 1101 */ Initializing b=13

int c = 0; Initializing c=0

c = a & b; /* 12 = 0000 1100 */ a=60= 0011 1100


b=13= 0000 1101
c=12= 0000 1100

System.out.println("a & b = " + c ); Prints c=12= 0000 1100

c = a | b; /* 61 = 0011 1101 */ a=60= 0011 1100


b=13= 0000 1101
c=61= 00111101
System.out.println("a | b = " + c ); Prints c=61= 00111101

c = a ^ b; /* 49 = 0011 0001 */ a=60= 0011 1100


b=13= 0000 1101
System.out.println("a ^ b = " + c );
c = ~a; /*-61 = 1100 0011 */ Opposite to a value

System.out.println("~a = " + c ); S.O.P will be 1100 0011

} Body Ends

Logical Operator
For example:

if(condition1 && condition2 && condition3)


If condition1 is true, condition 2 and 3 will NOT be checked.

if(condition1 & condition2 & condition3)


This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions,
you can get a good performance boost by using them.

AND Table
0 - FALSE
1 - TRUE
A B RESULT
0 0 0
0 1 0
1 0 0
1 1 1

Example for &:

Code Snippet Explanation


{ Body Starts
int a=10; Initializing a=10
int b=5; Initializing b=10
int c=20; Initializing c=10
System.out.println(a<b&&a++<c); false && true = false
System.out.println(a); 10 because second condition is not checked
System.out.println(a<b&a++<c); false && true = false
System.out.println(a); 11 because second condition is checked
} Body Ends
Example for ||:
For example:

if(condition1 || condition2 || condition3)


If condition1 is true, condition 2 and 3 will NOT be checked.

if(condition1 | condition2 | condition3)


This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions,
you can get a good performance boost by using them.

OR Table
0 - FALSE
1 - TRUE
A B RESULT
0 0 0
0 1 1
1 0 1
1 1 1

Code Snippet Explanation


{ Body Starts
int a=10; Initializing a=10
int b=5; Initializing b=5
int c=20; Initializing c=20
System.out.println(a>b||a<c);//true||true=true 10>5||10<20 i.e true||true=true
System.out.println(a>b|a<c);//true|true=true 10>5|10<20 i.e true||true=true
System.out.println(a>b||a++<c);//true||true = true 10>5||Assigned value is a=10 and it is
incremented value as 11 <20 i.e true||true=true
System.out.println(a);//10 because second condition is not checked Prints a=10
System.out.println(a>b|a++<c);//true|true = true 10>5|Assigned value is a=10 and it is incremented
value as 11 <20 i.e true||true=true
System.out.println(a);//11 because second condition is checked Prints a=11
}
Body Ends

Ternary Operator
Code Snippet Explanation
{ Body Starts
int a=2; Initializing a=2
int b=5; Initializing b=5
int min=(a<b)?a:b; Checks the condition whether a<b and produce result
which is minimum value
System.out.println(min); Prints minimum value
} Body Ends

Das könnte Ihnen auch gefallen