Beruflich Dokumente
Kultur Dokumente
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
{
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.
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();
}
}
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
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
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^=
|= <<= >>= >>>=
Unary Operator
Example 1:
Example 2:
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:
Shift Operator
Java Left Shift Operator Example:
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
} Body Ends
Logical Operator
For example:
AND Table
0 - FALSE
1 - TRUE
A B RESULT
0 0 0
0 1 0
1 0 0
1 1 1
OR Table
0 - FALSE
1 - TRUE
A B RESULT
0 0 0
0 1 1
1 0 1
1 1 1
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