new operator refers to the object creation where as instanceof used with the reference variable. Details of which we are going to discuss late in this topic. 

What is " new " Operator and its uses in Java ?

" new " is a keyword in java and it is used to allocate memory at the run-time and it the task of the JVM to allocate the memory or in other words we can say that it is used to create object which is responsible for allocating memory in java programming. 

Syntax for the new operator is :-

new (class_name)( [value] ) ;

The above statement indicates that with the help of new operator JVM is going to create a object of (class_ name) defined with it and that " value " indicates above is the way to pass the value while creating object to the constructor of its respective class. 

Another way of defining object by the use of new operator.

(class_name) (object_name) =new (class_name) ( [value] ) ;

In the above statement we are creating object through new operator and is the reference variable of that object and hence holds the reference value for that object. 

Here in the above syntax we are crating object of a user defined class that is why we are indicating (class_name) so that JVM should created object for that class only.

Reference value is used to indicate where the object is placed in the heap memory but don't misunderstood with this as an address because it is not a address indicating object but is a system generate random number as a reference value.

Note: we can create the object for any reference type variable like String, array etc .

What is " instanceof " Operator and its uses in Java
instanceof operator in java is only used for checking purpose whether a reference variable is holding the object / instance of specific type or not . It is a binary operator and it return only boolean value i.e true or false.


Conditions for using instanceof Operator

1> First operand must be reference type where as 2nd operand must be a reference type name ( class / interface ).

2> Type of reference variable and the specified type must be convertable and comparable i.e it will only allow to store the reference of its super type only.

for ex- Super class reference variable can hold the instance of its sub class but reverse it not true.
Inheritance property defines the convertibility. We cannot assign a instance of a class to the reference value of another class in a same level. 

In java both logical and bitwise operators are used. Logical operator returns the true / false value depends on the condition check and bitwise return the same true / false depends on the condition check but the difference between these operator is that logical operator only work with the boolean value where as bitwise operator work with both boolean and numeral values. How that will we see later in this post.

Logical operator

Logical operator in java only deals with the boolean value that means it will return either true or false depending on the condition check. If we try to use any other data type then it will give compilation error.

There are three types of logical operator i.e 
1> Logical AND  ( && )
2> Logical OR     (  | |  )

3> Logical NOT   (  !  )

Logical AND 

It is represented with the double ampersand ( && ) sign. basically it is a binary operator and both the operand must return either true or false value and no others data type are not acceptable with this operator.

Operand 1               Operand 2               Result 
   True                          True                     True 
   True                          False                    False
   False                         True                     False 
   False                         False                    False

Conclusion: If any of the operand is False then it will return False. 

Now think is there any need of checking if 1st operand is false ?
I think No because it is obvious that it will return false only but if 1st operand is true that means it may true or false so it is mandatory to check 2nd operand as well.


Note :- Compiler does the same that if 1st operand is false then no need to check further it further and hence save some compilation time.
for ex- 

if (  false && true && true && true ) // It will check the 1st operand if it is false then its obvious that whole statement will lead to false only so compiler will not check further. 
You can have any statement in place of true and false here but which returns either true or false value.

Logical OR

It is a binary operator and both the operand must return either true or false value and no others data type are not acceptable with this operator. It is represented like this   (  | |  ).

Operand 1               Operand 2              Result 
   True                          True                      True 
   True                          False                     True
   False                         True                      True 
   False                         False                     False

Conclusion: If any of the operand is True then it will return False
Now think is there any need of checking if 1st operand is true?
I think No because it is obvious that it will return True only but if 1st operand is false then there may be chance of returning true or false so it will be mandatory for compiler to check 2nd operand as well.

Note :- Compiler does the same thing that if 1st operand is true then no need to check it further and hence save some compilation time.

if (  true  | |  false  | |  true  | |  false ) // It will check the 1st operand if it is true then its obvious that whole statement will lead to true only so compiler will not check further. 
Here you can have any statement in place of true and false but it should return either true or false value.

Logical NOT

It is a unary operator and it will always return the result as reverse of its operand. Like if operand is true then it will return false and vice-versa.

Operand 1                    Result 
   True                             False 
   False                            True
  
Conclusion: If the operand is True then it will return False and vice-versa.                 
for ex-

if ( !  true ) // return false 
if ( !  false ) // return true

Bitwise Operator 

It is same as Logical operator but it can operate both with boolean and numeral value. There are four types of bitwise operator i.e
1> Bitwise AND    ( & )
2> Bitwise OR       (  |  )
3> Exclusive OR    ( ^  )
4> Bitwise NOT    ( ~  )    


Why named as bitwise ?

It is because for numeral values it deals bit by bit. How it deals bit by bit we will see later in this post.

Bitwise AND

It is a binary operator and represented with a single ampersand ( & ).It works same as logical operator in case of boolean value but how it works with numeral value is bit interesting .


How Bitwise AND works with the numeral value?

It deals with each bit that means it will result as 0 always and 1 when both the value are 0.Now think in zero's and one's. We all know that zero represent always false and one represent true always. 
Here it is also the same story that compiler will compare bit by bit and return the result accordingly.  
for ex-

System.out.println( (1 & 2 ) ) ; // It will display Zero as output.

1-> 01            
2-> 10             
---------
       00 ( 0 )
--------- 

Bitwise OR

It is a binary operator.It works same as logical operator in case of boolean value but how it works with numeral value is bit interesting . It is represented like this ( | ).

How Bitwise OR works with the numeral value?
It is same as Bitwise AND but only difference is it will result as 1 always and 0 when both the value are 0. 

System.out.println( (1  |  2 ) ) ; // It will display Three ( 3 ) as output.

1-> 01            
2-> 10             
---------
       11 ( 3 ) 
--------- 

Exclusive OR

It is a binary operator and represented with ( ^ ) this symbol. It result false when both the operand are either true or false otherwise true So compiler cannot be sure about result unless it checks 2nd operand. 

Operand 1               Operand 2             Result 
   True                          True                     False
   True                          False                    True
   False                         True                     True 
   False                         False                    False

Conclusion : It will return False if both the are operand are have same value either true or false otherwise true. It is same in case of bits also if both the bits are same either 1 or 0 then it will return 0 otherwise 1.

Have fun with code guys. keep experimenting!

This is most useful and confusing operator in java or any programming language. It is used to increase and decrease the value of a variable by 1. It is a Unary operator but the operand must be a variable containing numeral value and should not be a constant because constant cannot be modified in java i.e the final variable in java as a constant and direct value act as a constant.

for ex-
int    x  =  ++10 ;  // Invalid because 10 is a constant here. 

int    x  =  10;
x++  ; 
Above code is valid because here it increments a variable but (x++)++ is invalid because first it increments a variable then second time it contain a incremented value so it act like a constant and hence cannot be incremented further.

int    x  =  10 ;
(x++)++  ;     // This statement is equal to (10)++ which increments a constant value.
++(++x)  ;    // This statement is equal to ++(11) which increments a constant value.

Increments operator ( ++ ) 

This operator is used to increase the value of a variable by 1. Again it of two form i.e Post increment and Pre increment.

Post-fix operator : It is used to increment the value by one but in the next step when the variable is used again in our program . Currently it will always hold the current value but increment in the next step only, that is why it is named as post.
If you are confused with the wording then see the following example to make you more clear.
for ex-

int    x  =  10;
x++ ;               // In this  step it will hold the current value of x i.e 10.
System.out.println ( " Incremented value : " + x) ;  // If we try to print or use " x " then it will print or use incremented value.

Pre-fix operator : It is used to increment the value by one in the current step only that is why it is named as pre. If you are confused with the wording then see the following example to make you more clear.
for ex-

int    x  =  10 ;
++x ;                // In this  step it will hold the incremented value of x i.e 11.
System.out.println ( " Incremented value : " + x) ; // If we try to print or use " x " then it will print or use the previously incremented value. 

Now you must be wonder to see that both are having the same output then what is the difference. Hole on and see this example, will make everything clear about post and pre.

int    x  =  10;
int    y  =  10;
System.out.println(" Incremented value : " + (x++)) ; 
System.out.println(" Incremented value : " + (++y)) ; 

Now try it execute the above statement it will show you the output as 10 and 11. Now you can conclude that post will first hold the value and then increment it where as pre will increment the value at the same time.

int    x  =  10;
System.out.println(" Incremented value : " + (x++)) ; 
System.out.println(" Incremented value : " + (++x)) ; 

Here the output will be 10 and 12 at the first print step it will hold the current value i.e 10 and then in the in step it will increment it 11 and then it will increment to 12 because here in the 2nd print statement there is a pre increment which make it to 12.
Hence 11 because of post and then 12 because of pre increment is used.

Decrements operator ( ++ )  

This operator is used to decrease the value of a variable by 1. Again it of two form i.e Post decrements and Pre decrements

Post Decrements : It is used to decrements the value by one but  in the next step when the variable is used again in our program. Currently it will always hold the current value but decrements in the next step that is why it is named as post.
If you are confused with the wording then see the following example to make you more clear.
for ex-

int    x  =  20 ;
x-- ;                  // In this  step it will hold the current value of x i.e 20.
System.out.println ( " Incremented value : " + x) ; // If we try to print or use " x " then it will print or use Decremented value i.e 19

Pre Decrements : It is used to decrements the value by one in the current step, that is why it is named as pre.
See the following example to clear out all your doubts.
for ex-

int    x  =  10;
--x ;                  // In this  step it will hold the incremented value of x i.e 9.
System.out.println ( " Incremented value : " + x) ; // If we try to print or use " x " then it will print or use the previously decremented value. 

No need to wonder because here it is working same as it was in increments operator.You will see the same difference but the difference is that here value will get decreased. See the following example.

int    x  =  10;
int    y  =  10;
System.out.println(" Incremented value : " + (x--)) ; 
System.out.println(" Incremented value : " + (--y)) ; 

Now try it execute the above statement it will show you the output as 10 and 9. Now you can conclude that post will first hold the value and then increment it where as pre will increment the value at the same time.

int    x  =  10;
System.out.println(" Incremented value : " + (x--)) ; 
System.out.println(" Incremented value : " + (--x)) ; 

Here the output will be 10 and 8 at the first print step it will hold the current value i.e 10 and then in the in step it will increment it 9 and then it will increment to 8 because here in the 2nd print statement there is a pre increment which make it to 8.
Hence 9 because of post and then 8 because of pre decrements is used.

Now try to execute more using more increment operator in a single steps like

int  y  =  10 ;
y+++(++y)+(++y)+(++y) ; 

It will give output as 49 think why ans try to find which way it is executing whether it is executing form left to right or right to left.

Relational Operator in Java is used where we use any comparison between two value or two variable or a value and a variable, but while comparing variables , it must some numeral value and hence all the operators used for comparison called as comparison operators. 

On the other side there are some operator used for comparing equality are called as equality operator.

So finally Relational operator compromise of two operators i.e Comparison operator and Equality operator.

Comparison Operator, its types and uses 

As said above its only use is to compare either direct values or through variables.It is generally used with the control statement like if , while and do-while and with conditional operator as well. 

Why it is used with only these control statement ?
--> It is because these control statement on the condition whether it is true or false and this operator return true or false based on the condition check.

There are four types of Comparison Operators. they are:-
1> Greater than operator ( > ) :- It is used to compare whether the 1st operand is greater than the 2nd operand or not.
for ex -

if ( 20 > 10 )  // it will return true because the condition is true. 

while ( 20 > 10 )  // Here condition is true and loop will run infinite times to avoid we can 

                                use break statement inside the loop.

int   x  =  20 ;
int   y  =  10 ;
if ( x > y )
Here we are using variables for comparison. It will work fine and compiler and run successfully. 

1> Greater than and Equal to operator ( >= ) :- It is used to compare whether the 1st operand is greater than or Equal to the 2nd operand or not.Only difference between " > " and " >= " is that with the >= . We can be able to compare for the equality of the value as well.

for ex- You can prefer the above example simply change the operator and see the difference.

1> Less than operator ( < ) :- It is used to compare whether the 1st operand is less than the 2nd operand  or not.
for ex -

if ( 20 < 10 )  // It will return false because here the condition is false. 

while ( 20 < 10 )  // Here condition is false and loop will not run at all .

int   x  =  20 ;
int   y  =  10 ;
if ( x < y )
Here we are using variables for comparison. It will work fine and compiler and run successfully. 

1> Less than and Equal to operator ( <= ) :- It is used to compare whether the 1st operand is less than or Equal to the 2nd operand or not.Only difference between " < " and " <= " is that with the <= we can be able to compare for the equality of the value as well.

for ex- You can prefer the above example simply change the operator and see the difference.

Note:- You can use directly all these operator in the print statement to check whether it is returning true or false.
for ex-
System.out.println(20>10); //It will print true because the condition defined is true here.

System.out.println(20<10); //It will print false because the condition defined is false here.

Equality Operator, its types and uses 

Equality operators are used to check whether the value is equal or not. But we can't be able to compare two strings with the help of equality operator because String is a class in java so when ever we are defining any string compiler will create the reference for that string and then reference value is stored in the String variable. And if we are comparing those variable then internally it is comparing the reference variable not the assigned string to that variable.Details of which we will see in String topic later in this tutorial.

Note :- In java we have some predefined functions here " equals() " is used for string comparison.

There are two types of quality operator in Java :-
1> Equal to operator ( == ) :- As said above it only checks whether two values or variables are " equal " or not and it will return true or false according to the condition check.

for ex -
if ( 20 == 10 )  // It will return false because the condition is false. 

String    s  = " Java "  ;
String    s1  = " Java " ; 
System.out.println ( " Comparison result : " + ( s == s1 ) )  ;

It will compile successfully but it will display true , you must be wondering that you above i said it will not compare the string but here it is giving true, but my friend it is not true because of "String value" but because of reference value which is stored in " s " and " s1 ". 
I am sure you again wondering how reference will be same if we are defining two different string type variable. It is because compiler will check whether the String value is same or not , if it is same then it will store the same reference value in " s1 " as in " s " .

Note :- In the above code if you create object of the string type then it will return false whether the string defined is same or not because by creating object both the reference variable s and s1 contain different reference value because its a matter of object and we are creating two object because every object has it own memory and own property.Like this, 

String    s  =  new String("Java")  ;
String    s1  =  new String("Java")  ;
System.out.println ( " Comparison result : " + ( s == s1 ) )  ;

If we compiler and run the above code it will return false. Reason as said above as a note.

2>Not Equal to operator ( != ) :- It checks whether the two value or variable is " not equal " or not and it will return true or false according to the condition check.

for ex -
if ( 20 != 10 )  // It will return false because the condition is false. 



It is a kind of operator which has a wide use in any programming language and as it name suggest it is used to assign value to a variable and that value may be a direct value or a variable or we can say that it is used to assign value in the memory. That means there are two of assigning value to a variable .It a binary operator.

Syntax:-
<variable_name> = < value >;

1> Direct way :- It means we are directly assigning value to its typed variable.
for ex-
int    x  = 10 ;
float    x  = 10 ;
char   x  = 'A' ;

1> Indirect way :- It means we are Indirectly assigning value to its typed variable.Means we are assigning a variable to another variable. We are using this concept while swapping two numbers.
for ex-
int    y  = 10 ;
int    x  = y ;

Note:  While assigning make sure that the value should be assignment compatible to the variable type.
That means any integer value can be store to any int type variable it may be byte,short,int or long. 
And also make sure about the range of any  ' type ' because value must be within the range of variable type.

If we want to store the int value in the byte ,char or short then we must take care about the range.
for ex- Byte can't store any integer value below or above its range . Means it cannot store below ( -127 ) and above ( 128 ).
for ex-
byte   y  = 129 ; // Compilation error , 129 is not in range of byte type.

While assigning any value  to the variable also take care of memory , i means to say that if you need to store value " 128 " then it can easily accommodate in byte a variable so don't take a habit of using int only because unnecessarily it will occupy more memory.  

Let me tell you some of the useful and interesting cases while assigning any value to a variable .

1> Can't store a integer value to a byte type even if result value of within its range. 
byte   x  = 20 ;
byte   y  =  x   +  10 ;

While compiling the above code it will give compilation error. Now think why?
It is because here in the 2nd line we are adding a  byte value to the a int type and int type is a wider type as compared to byte type so the return value will be a int type, so compiler will search for a int type variable but here it is a byte so we cannot store a integer value to a byte even if the value is within its range. 

In java by by default any integer value is int type. 

2> We cannot store a two byte value in a byte type if it exceeds its range.
byte   x  = 200 ;
byte   y  = 20 ;
byte   z  =  x   +  y ;

It will also with compilation error because the result value after adding is " 200 " which exceeds its range. So even if two byte value cannot accommodate into a result byte type if it exceeds its range.  

So like above there are many cases you come across while coding. 
Interestingly, if we define any value as a constant which is with the use of " final " keyword in java then we will come across some interesting fact while storing values. I mean to say that here in the first case, 10 is by default int but if we make to it a constant then that code will not give any compilation error, we will see in details in the final keyword topic later in this tutorial. 

After 1 to 2 hour searching i went to a right place where i find my solution about "how to create a triangular shape using CSS". When you see the solution you will be in shock that "for what i spend 2 hour".
Actually i was working on a project where the need is to create a voting scheme like stackoverflow.com have.
So here is the simple code to create a traingular shape using CSS.

HTML CODE

<html>
    <head>
        <link rel="stylesheet" href="some.css"/>
    </head>
    <body>
        <div class="top"></div>
        <div class="left"></div>
        <div class="right"></div>
        <div class="bottom"></div>
    </body>
</html> 
CSS CODE

.top {
 height:0px;
 width:0px;
 border-right:40px transparent solid;
 border-left:40px transparent solid;
 border-bottom:40px red solid;
}
.bottom {
 height:0px;
 width:0px;
 border-right:40px transparent solid;
 border-left:40px transparent solid;
 border-top:40px red solid;
}
.right {
 height:0px;
 width:0px;
 border-top:40px transparent solid;
 border-left:40px red solid;
 border-bottom:40px transparent solid;
}
.left {
 height:0px;
 width:0px;
 border-top:40px transparent solid;
 border-right:40px red solid;
 border-bottom:40px transparent solid;
}
So the output will look like this
So here you done. If you want to create the voating scheme like stackoverflow you can use these divs like an anchor.

In java " + " symbol is used to concatenate two strings where as in case numerals it will add up the values.
It has wide use as an String concatenation. String concatenation means adding/ merging two or more than two string values. In java String are represented in double quote ( " " ) and the any value within this quote will be treated as a string it may a number, character or string ( Collection of characters ).

While concatenating any values if any of the value is string type then the result type will be string. And result type is int only when if both the values are integer type.

Let us discuss some cases which you may come across while reading about string concatenation operator and some of its uses.

1> Concatenating two string value :- Here we are concatenating two string values whose result is also a string type. And that string may be a character , or a numeral value or a string which is a collection of character.

for ex-
String  s  = " Java " + " World " ; // Concatenating two string values and storing it in a
                                                             string type
" s " .

String  s  = " Java " + " 10 " ;  // Concatenating two string values where one string is a
                                                        number and
storing it in a string type
" s " .

String  s  = " 20 " + " 10 " ;  // Concatenating two string values where both the strings are
                                                    number and storing it in a string type
" s " .

String  s  = " Java " + " C " ;  // Concatenating two string values where one is a character
                                                      as a strings and storing it in a string type
" s " .

String  s  = " 10 " + " C " ;  // Concatenating two string values where one is a character
                                                  as a strings and another one is number as a string and
                                                  storing it in a string type
" s ".

2> Concatenating String with a numeral value :- We can concatenate a string with a number but the result will be of string type.

for ex-
String  s  = " Java " + 10 ; // Here " 10 " append with the Java string and result will be
                                                a string i.e
 " Java10".

When we compile the above statement then compiler will convert the number into a string type by overriding toString() .  We see in the details about over riding and how it is converted internally into string by the compiler in String topic later in this tutorial.

String  s  = " " + 10 ; // Here also" 10 " is converted into a string because it is
                                       concatenating with a empty string. 


2> Concatenating String with a character value :- We can concatenate a string with a character value as well but the result will be of string type.

for ex-
String  s  = " Java " +'C' ; // Here " C " append with the "Java" string and result will be a string i.e "                                                           JavaC".

String  s  = " Java " +C ; // It gives you the compilation error because here Compiler
                                              search
C as a variable because here it is not a character .
                                              To represent a character must be defined under single quote.

Note: But we try to concatenate a character with a number then its result type will be integer or a character.
It is because every character has a ASCII value and compiler always deals with the ASCII value of a character Ans the result type will be depends up the data type we have used for storing the result. 

If want to integer value after concatenating a character and a number then store it in int type and if we want to display a character then store it in char type.

for ex-
int  s  = 'C'+10 ; // Here it will display 77 because the ASCII value of C is 67 and after
                               adding 10 it will display 77.

char s  = 'C'+10 ; // Here it will display ' M ' because the character value for 77 is ' M '. 
Basically we can treat operators in three category on the basis of operand used with the operators i.e
1> Unary Operator
2> Binary Operator
3> Ternary Operator

Unary Operator 

It is the operator used with the single operand . As its name indicate " unary " that means " single ".
Some of the unary operators are increments ( ++ ) and decrements ( -- ) operators. We will study in details about these operators in their respective topic later in this tutorial.

We can also use " + " operator as unary operator which is in java formally called as " string concatenation operator ".
for ex-

int   x  =  + 10 ; // Here x represents +ve 10 value but itself by default it is +ve. 

byte   b =  +x  ; // Invalid. Here it will give compilation error because we are storing int                                    value " x " in byte type"  b " so be careful by assigning any variable to
                             another variable.


int   x  =  -10 ;     // valid statement represent -10. 

byte   b  =  -x;     // invalid same reason as above.

int    x  =  ++10 ; // Here ++ represents the increments operator .

int    x  =  --10 ;   // Here -- represents the decrements operator.


Binary Operator 

It is the operator used with two operands and we all know that binary represent two. So we need to operate with this kind of operator.
For example we are using following operator as an binary operator.

String concatenation operator ( + ), Assignment Operator ( = ), Relational Operator ( Both equality and comparison operators ) , Logical operators , Bitwise operators  and shift operators. 

int    x  =  10 + 20 ; // Here" + " represents the use of Binary operator .

int    x = 20 ; // Here " = " is an Assignment operator used left side as an variable and                        
                         right side is a value of it.


if ( 10 < 20 ) // Here " < " is an Comparison operator , which returns true or false and it                  
                        must have 
value as an operand not variable as an operand .  

if ( 10 >= 20 ) // Here " >= " is also an Comparison operator , which returns true or false                
                         and it must have value as an operand 
not variable as an operand .

if ( 10 == 10) //Here " == " is also an Equality operator , which returns true or false and it                
                        must have 
as an operand not variable as an operand . 

if ( 10 != 10) //Here " != " is also an Equality operator , which returns true or false and it                  
                        must have value as an operand not variable as an operand . 


Ternary Operators 

It a kind of operator which is used with three operands for example conditional operator is one of the ternary operator.

Syntax :-
<operand 1 >  ?  <operand 2 >   :  <operand 3 >
Here the condition is < operand 1 > must return boolean value i.e true or false .
If < operand 1> returns true then < operand 2> will be displayed as an output and if false then < operand 3> will be displayed as an output.

for ex-
int   x  =  ( 20 > 10 )  ?  20  : 10 ; // here condition is true that means 20 is displayed as an output.

Literals in java define the actual value we are using for variables, constants or to perform any operation.
There are seven literals in java in which one literals is introduced from java 7 i.e binary literals. They are as follows:-
1> Boolean literals                  2> Character literals
3> String literals                      4> Integral Literals
5> Floating literals                   6> Null literals
7> Binary literals ( from java 7)

Lets discuss more in details about these literals one by one.

Boolean Literals 

For boolean literals there are two possible values to be used with the variables or the constants are true or false. In java we can't use " 0 " as a " false " value and " 1 " as a " true " value because in java any numeral value is considered as int type, that means 0 and 1 doesn't return any true or false value. But the same was true in case of other programming language like C where " 0 " return the " false " value and "1 " return the " true " value. This make java different from other programming language like C or C++ .
In short integral value can not be used as boolean in java and the default value of boolean is " false ".

Character literals

Character literals in java represents the representation of single character used within the single quote and these literals is used where char data type is used. There should be only one character available inside the single quote and must not be empty.

For ex-   char  c = 'A' ; // valid statement

We can also used ASCII with the char variable like,

char c = 65;
When we compiler the above statement the compiler convert it into the following statement,
char c='A';

So from the above statement we can conclude that ASCII value can be used with char variable which will be again internally converted to its respective character by the compiler.

Note: ASCII range is from 0-255 ( 8 bit ) but previously it was 0-127 ( 7 bit ) .

String literals

String is the collection of character or sequence of character which must be enclosed the double quotes. In java any value is within double quote treated as a string value.
String is a class defined in java programming language.
The default value of any string literal is NULL . String variable is a reference type whose size is 8 byte and will store the reference of the value defined as a string type.

For ex-  String str = " Java ". 

In the above statement str is a string variable of reference type which store the reference of the " Java " which is a string value. Storing reference means it will store the integral value where the value of the string variable is stored. But don't misunderstood that it is a address. It never be a address and hence just a system generated integral value . Java never deal in address with the user, but internally uses pointer and addresses.

Integral literals 

These literals represents the integral value and will be used in the following four data types,
byte,  short, int and long.
As per the size and data type used,  the value for the variable will be stored.
By default the ' type '  for integral literal will be int type and the default value is zero(0).
If you are using value of the variable as a long type then we have to append ' l ' or ' L ' at the end of the value.
for ex - long x = 12398765540987L;
Integral literals are represented in following three forms:-
1> Binary literals ( from java 7)
2> Octal literal
3> Decimal Literal.
4> Hexadecimal literals

Binary literals

It is the new concept introduced in java with the release of java 7. From java 7 we can be able to represent binary value to the integral literals with the use of 0 and 1.
To represent the data in binary the value must start with either ob or oB.

Octal literals

It is concept for representing the literal value in the octal form with the use of digits from 0 to 7.
To represent the data in octal from the value must start with a zero (0).
For ex-
int  x= 010;
010---> represented in octal form and represent 8 in decimal.
10----> it the decimal representation of the integral literals.

Decimal literals

It is the concept of representing decimal value to the variable with the use of 0 to 9.
for ex- int x =  10;

Hexadecimal literals

It the concept of representing the value in the hexadecimal format with the use of 0-9 , a/A to f/F .
To represent the hexadecimal number it must be start with 0x/0X.
for ex-
int x = 0x41; // 0x41 represent 65 in decimal .

Floating literals

These literals represents the numerical value can be used with two floating data type , they are float and double.

By default the it is double but if we are using any floating literal with float type then we have to explicitly mention ' f / F ' at the end of value.
for ex -
float x = 12.34f ; // Here it is float type 
float x = 12.34 ; // Here it is by default double type


Null literals

Null is a default value for any reference type. If the value of the reference type is null then it indicate no reference/address are available in the variable.

for ex-
String str=NULL; // indicate str dose not contain any reference but contain NULL.

As you all know that data type are the keywords or we can say that it defines the memory assignment  for different "types" available in java.
The data types which are predefined in java is called as keywords.

For ex- int, float, byte etc
On the other side the data types which are defined by user are called as "User defined Data types".
for ex- class, interface etc 
And as per the data representation we can categories it into two types:-
1) Primitive/Basic Data Type.
2) Reference Data Types.

Primitive Data Types

These are the predefined keywords in java.It generally indicates the memory size allocation and types definition for the variables defined in java. If we are defining and primitive data type in our program JVM is going to allocation memory according to the data types defined and then in the allocated memory the actual value will be stored. There are 8 types of primitive data types in java:-

boolean
byte            char              short           int        long
float            double

Boolean :- Boolean in java a one kind of data type which only define the true and false i.e whether this condition is true or not and return only true and false. All the letter should be written in lower case (boolean).
Default value for boolean data type is  " false ".

Note :- It is not going to return 0(false) and 1(true). It is a bit different from  c or c++ .

For ex- if ( 1 ) // invalid in java , but valid in c (1 indicate true in c but in java
                          it is treated as int type )
           if( true ) // valid in java. 

Byte :- Byte is a lower form of int data type. Here lower means, for this JVM is going to allocate less memory i.e 1 byte ( 8 bit ) . Default value for byte data type is " 0 ". And we can define it as both signed and unsigned . That means it can store negative (-ve ) value as well. All the letter should be written in lower case (byte).
Range for byte :- -2(-128) to 2 7-1 (127) .

byte b=129; // invalid because it cannot store less than (-128) and greater than (127).
byte b=126; // valid statement because within the range.

Char :- Char is a data type which is used to store single character within single quote ( ' ' ). JVM will allocate  2 byte ( 16 bit ) memory for "char" . Default value for byte data type is ASCII - 0. And we can define it as only unsigned . That means it can store only positive (+ve ) value. All the letter should be written in lower case (char).

Note:- Assigning any char value to a variable means internally the ASCII value for the character is stored in the 2 byte memory space. If we want we can print its ASCII value by type casting that char variable into int type.
Range for char :- 0 to 2 16-1 (65535) .

char c='8' ;  //  valid and will print the value 8.
char c='11'; // Invalid and give error because in the previous example internally it stores
                        the ASCII of 8 , ASCII for numerals are available from (0-9).



Short :-Short is also a lower form of int data type. Here JVM will allocate 2 byte ( 16 bit ) memory . Default value for byte data type is " 0 ". And we can define it as both signed and unsigned . All the letter should be written in lower case (short).
Range for short :- -215  to 2 15- 1 .

Int :-Int is Integer data type. Here JVM will allocate 4 byte ( 32 bit ) memory . Default value for byte data type is " 0 ". And we can define it as both signed and unsigned . All the letter should be written in lower case (int).
Range for int :- -231  to 2 31- 1 .

Long :-Long is wider int type. Here JVM will allocate 8 byte ( 64 bit ) memory . Default value for byte data type is " 0 ". And we can define it as both signed and unsigned . All the letter should be written in lower case (long).We have to append 'l / L' at the end of floating value.
Range for long :- -263  to 2 63- 1 .

Float :-Float is Floating data type that means it can store decimal values. Here JVM will allocate 4 byte ( 32 bit ) memory . Default value for byte data type is " 0.0 ". All the letter should be written in lower case (float).We have to append 'f / F' at the end of floating value.
syntax :- float f = 12.34 f ;
Range for float :- No range because of storing decimal value. We can have infinite decimal value between 0 and 1 so think it has 4 byte memory (32-bit) that means it can have infinite number between these range.

Double :-Double is wider Floating type that means it can store decimal values. Here JVM will allocate 8 byte ( 64 bit ) memory . Default value for byte data type is " 0.0 ". All the letter should be written in lower case (double).
Range for double :-Like floating type it has also no range. 

Reference Data Types

It is the identifier like class, interface/array/string etc in java. If we are defining any reference type then the memory allocated for the reference type is 8 byte always and it will store reference of that reference type rather then any value. But reference is itself in the form of integer value but don't misunderstood that it is storing address for reference type. Address concept in java is abstract.The default value for any reference type is NULL .
Few of the reference type are :-

String , Object, array etc
This is just the introduction to reference type , details of which we will see in their respective topics. 

Import statement in java is used in java to import all the predefined classes defined in a package and within that classes many methods are defined. Package is just like the directory in the file system.  Whenever we are writing any import statement means we are telling compiler to load all the classes or any specific class, it depends upon the import statement declaration.User can use any class or that specific class at any moment of time in their programs. Now i will tell you how to use import statement and how to import all the classes defined in different packages or any particular classes from a package.
Java languages follow the following sequence of definition.

Package ----- > Class -------> Methods

The above diagram indicates that packages contain many classes and then within those classes there are many predefined methods.

Now you must be wondering how java follow this type of structure?
It is just because to make it object oriented, we are calling these objects with its class name where it is been defined but if we define our own class and if it contain some methods then we need to create object to call all our methods. Again if method is defined as static then we can call it by its class name also, that is the reason why almost maximum predefined functions are declared as static so that we can use it just by its class name.
All import statement must be terminated with semi-colon ( ; ).

There are different ways of importing packages :-

1> Importing all the classes within a package.
<package_name>.* ;
for ex- java.lang.* ;
Here java.lang is a package definition and the asterisk character is used here to indicate the compiler to import all the predefined classes from that package.

2> Importing specific class from a package.
<package_name>.<class_name> ;
for ex- java.lang.Object ;
Here java.lang is a package name and Object is one of the class within that package. So here we are telling compiler that we want to use only Object class and we can use the methods defined in this class any where in our program.

Like java.lang there are many other packages.
for ex- java.swing, java.applet, java.awt, java.io, java.beans, java.math etc
and also
javax.sequrity, javax.swing, java.sql,javax.sound etc
any more packages are there .
If you want you can have a look on those packages and classes. Follow the following steps.

1> Go to that Drive where you have installed your JDK. In my PC i have installed in C:\ drive.
2>Now select the program files folder and double click on it.
3> now you find a java folder ,double click on it.
4>Now you will find two folder i.e JDK and JRE
5> Go to JDK folder where you will find a src folder.
6> In src folder you find various packages like java, com, javax etc and within that packages many classes defined.
7> Now have a smile in you face that you got the predefined classes that you are using in your programs.

In short follow this sequence diagram,

C: Drive ---> Program Files ----> Java Folder ---> JDK ----> src folder----> many packages like java, javax etc 

Now have fun with the import statement. Import any one and build your apps accordingly. 

In this post i will tell you something about compilation which is a task of compiler and on other side interpretation which is the task of interpreter. 

There are few queries related to this topic which i am going to answer in this post and will try to make you more clear about this topic. Some questions are as follows :-


Why compilation and what is the need of compilation ?

Why interpretation, what is the need of interpretation and who is responsible for it ?

Why interpretation followed by compilation in java why not the vice-versa ? 
What is the benefit of introducing both compilation and interpretation in java?

What is Compilation and its need in java 
Compilation is the fist step while executing the java program. Java compiler (javac) is responsible for this.Like all other programming languages here in java also compilation does the same thing , simply checking for the syntax error( any error related to the definition and declaration of variables. method,classes and so on). In other word we can say compiler is not involve in any issue related to memory, it just check whether the user have followed the correct sequence and syntax or not. It not then it will compilation error. 

Compilation syntax:-
javac  <file_name> . java

More interestingly in java compiler create a .class file after successful compilation. And .class is created for each and every class defined in the program.

What is .class file and what does it contain?

.class file is created by the compiler and it contain the same code you are written but in optimized way and is is converted into byte code which is JVM readable.
You can also check the content in the .class created by the compiler use a decompiler, which is responsible for decompiling the compiled code so that a user can have a view how compiler is compiling and optimizing the source code. Let me show you the creation of .class file with the help of an example. 

For ex-

import java.lang.Object;

class A{}

class B{}

public class C{
public static void main(String str[]){

System.out.println("main() method in class C");

  }

}
Here in the above there are three class A, B and C where A and B is not having and definition and declaration and C has main() method a print statement . 
After successful compilation, three .class file will be created like this,
A.class
B.class
C.class

After creating the .class file the interpreter play its role by interpreting  .class files. 
Here in the above example three .class files are created which are going to be read by the JVM( Java Virtual Machine). For now only remember that JVM is responsible for memory assignment of the declared variables and members in class, details of which we will see in their respective topics. 

What is interpretation in java?

Interpretation is the 2nd step for the execution of java program, which is always followed by compilation.

Java is responsible for interpreting the .class file. Actually java is formally known as java interpreter and is a executable file means it is present in the JDK in the bin directory named as java.exe. 

Interpretation syntax :-
java  <class_name> 

Must notice that here we can give the name of any class we have defined in our source file. Lets consider the above example with little modification. 


import java.lang.Object;

class A{

public static void main(String sush[]) {

System.out.println(" class A main() method ");

    }

}

class B{

public static void main(String sush[]) {

System.out.println(" class B main() method ");

  }

}

public class C{
public static void main(String str[]){

System.out.println(" class C main() method ");

  }

}


Save it as C.java.

Now compile the above code as, 
javac C.java because C class is public. Three .class file will be created for each class A, B and C, like this
A.class
B.class
C.class

Now for interpreter there are three option means , it can execute any class. And notice here that all these classes are independent from each other means the respective classes are executed by its name only. 

It not like that if you execute by the name of class C and will execute all other classes.
for ex-

java C ( it will execute and print the content of main() method available in class C only)
output:- class C main() method

java  B ( it will execute and print the content of main() method available in class B only)
output:- class B main() method

java  A ( it will execute and print the content of main() method available in class B only)
output:- class A main() method

So you have option while interpreting that you can execute any class you want. That is the reason why we are only writing class name for executing our class because JVM while search according the class name which you had compiled.  

Need of Interpretation in java ?

Need of interpretation is with memory assignment . After compilation the only job left is to assign memory for the variable defined in the class to assign memory for it and it will be done by JVM only. Which read the .class file and assign the memory for the defined members. 
Unless we interpret we are not going to get our desired result on the screen. 

Why interpretation followed by compilation in java not the vice-versa ? 

It is because to make java as a platform independent language. 
After compilation .class files are created and these .class files are portable, that means we can carry these class files and can able to execute in any platform, only condition we need to follow is to must have JVM installed on that platform . This is also a reason why java is called as " write once and run anywhere ".
Compilation must be the first step because it creates .class file and which makes java a platform independent language and defines the potability of code . It is not followed like that all these facilities will get violated. 
This is the benefit of introducing compilation and interpretation in java. 
In this post i will tell you something interesting about main() method in java. Like
What is the use of main() method?
How we are going to use main() method?
Why we are using main() method in our java program?

You will find the answers for all these questions in this post only.

 What is the use of main() method  in java ?
Basically we can say that it is the starting point for the java program.
Let suppose there is no main() method in your program and you have many definition and declarations in your program then from where the JVM will start executing your java program ?

---> Your JVM will confused about starting point of the java program. So to be not a part of any confusion, java developers sets that main() method would be the starting point of any java program. Still we can execute without main method but JVM will throw run-time exception saying that "java.lang.NoSuchMethodError" . But note that it is not a compilation error but is a run-time exception.

Let me show you an example showing the above mentioned exception:-


import java.lang.Object;

public class FirstProgram {} // This class is without main()method


save it as FirstProgram.java and run the code you will get a run-time exception like this,










But in java if you define any static block then priority will be given to that block then to the main() method.
let me make you more clear regrading the above statement with the help of an example.


import java.lang.Object;

public class FirstProgram {

static {

System.out.println(" This is static block ");//print statement in static block

}

public static void main(String str[]) {

System.out.println(" This is main() method ");//print statement in main()

  }

}

Here in the above program we have a static block and a main() method. If you see the output of this program it is a bit interesting.
Output -- This a static block
               This is a main() method

Here you can see static block is executed first but why?
--> Reason is that, static is the scope of class and all the static variables are initiated / loaded at the class loading time that means before executing the program. JVM is responsible for loading the .class file. And the program will be executed after loading the .class file. To know about static Click Here !  and for class loading just Click Here ! .

If we are thinking of executing program having static block but no main() method?
--> Its simple, you can run your program with above mentioned condition but only difference is that it will print the content of static block first then will give run-time exception "java.lang.NoSuchMethodError".

Now you can verify that class loading is done first then execution will start. Means JVM read the static block before executing the program. That is why it is printing the content of static block first and then giving run-time exception about main() method.

For confirmation of above statement try to run the above program without main() method. The output will look like this,

Exception in main" no such method "








How we are using main() method in java ?
It has got some syntax i.e ,

public static void main(String str[ ]){ }

Here you can see that public and static both are modifiers and there is no arrangement for modifiers declarations in java, we can use like this also,

static public void main(String str[ ]){ }

The above syntax will work fine like the 1st statement.

Why it is static ?
-->It is because just above i showed you that static block will be initialized while loading the .class file. So main() is a static method so your code can run freely without the involvement/creation of any object.

And if you missed out writing public then while executing, it will show a message -
" main method not public "
.

void :- void indicates the return type of the main() method, that is , it is not going to return any value.

main(String str[]) :- Arguments with the main() method indicate that you can pass any String value while executing your program through command prompt . Here you can see that it is String type that means you can pass a string value and the variable str[] is a array type, it means that it can accept any number of argument. Passing value at the time of execution of the java program is called as command-line-argument.

Try to print the length of the str[ ] variable. Like this,

import java.lang.Object;     // import statement 

public class FirstProgram {

public static void main(String str[]) {

System.out.println(" The length of the str variable is :" +str.length);

  }
}

Save it as FirstProgram.java then compile and run will give the output as,

Output:- The length of the str variable is : 0 

Details about how to pass and print the value just Click Here ! .

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic