Java Data Types
--
Hi readers! I do hope You read my first article.It is about Introduction to Java.Here I bring you my second article.It is about Java Data Types.
Data types
There are mainly two kind of Data Types in Java.
- Primitive Data Type-
Integer,Character,Boolean,Floating
2. Non primitive Data Type-
Classes,Interfaces,Arrays
This is how data types conclude capacity.
Variable Deceleration
ex:
int a
char ‘ch’
Let us see some Examples
1.Integer
public class FirstProgramme{
public static void main(String args[]){int a=777;
int result=a*2;System.out.println(result);
here we get the answer as 1554.
2.Long
public class FirstProgramme{
public static void main(String args[]){long a=7773334445556661;
long result=a*2;System.out.println(result);
}
}
here we get the result as 1554666889111332.If we declare here a as int there it says that integer number is too large.So it doesn’t run.long consists with 8 bytes.So here we declare a as long.Also at the end of the number here we apply l.
Concatenate
public class FirstProgramme{
public static void main(String args[]){long a=222223334445551;
long result=a*2;System.out.println(a+""+result);
}}
Here we get the answer as 2222233344455544444666889110. Here result combined with a.
Now we see how can we get our result in hexadecimal form.In hexadecimal it consists with 1,2,3,4,5,6,7,8,9,A,B,C,D,E.
let us get 1B2E . this is in hexadecimal form.
public class FirstProgramme{
public static void main(String args[]){int value=0x1B2E;System.out.println(value);
}
}
Here there 0x should be there to convert this hexadecimal into decimal.Here we get answer as 6958.
3.Float
public class FirstProgramme{
public static void main(String args[]){float value=22.678f;System.out.println(value);
}
}
Here the answer is 22.678
public class FirstProgramme{
public static void main(String args[]){float r=22.678905;
float areaOfCircle=3.14f*r*r;System.out.println(areaOfCircle);
}
}
Here we get the answer as 1615.005.If we put double here answer may change as 1615.0049018014715. When we use float the value estimate for two or three decimal point values.But double doesn’t. Here we can see the differences between float and double.In int,float,double they doesn’t represent letters.So we use characters and Strings in this case.
4.Char
public class FirstProgramme{
public static void main(String args[]){char ch='a';System.out.println(ch);
}
}
In char here we show only one character. It is written in with single quotation.In characters.But in string we use double quotations.
5.String
public class FirstProgramme{
public static void main(String args[]){String a="Sri Lanka";System.out.println(a);
}
}
Here we get the answer as Sri Lanka.
public class FirstProgramme{
public static void main(String args[]){char ch='A';
System.out.println(ch+);
}
}
Here we get the answer as 66.Because we add here 1 for the ascii value of A.The ascii value of A is 65.
Escape Sequences
\t-tab
\b-backspace
\n-New line
\’-Single quote
\”-Double quote
\\-Back slash
public class FirstProgramme{
public static void main(String args[]){System.out.println("Sri \t\t\t\t Lanka");
System.out.println("Sri \b Lanka");
System.out.println("Sri \n Lanka");
System.out.println("Sri \' Lanka");
System.out.println("Sri \" Lanka");
System.out.println("Sri \\ Lanka");
}
}
When we use above sequences,The result as follows.
Scanner
In java programming here we use scanner to read data.
Scanner sc=new Scanner(System.in)
import java.util.Scanner;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);System.out.println("enter number");
int num=sc.nextInt();System.out.println("enter mobile number");
long mobileNo=sc.nextLong();System.out.println("Enter Bonus");
float number=sc.nextFloat();}
}
At top of the program we write as, import java.util.Scanner
when we read the result.
In here we get numerical data input.byte,short and float can be read.
import java.util.Scanner;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);System.out.println("Enter your name");
String name=sc.nextLine();System.out.println("Enter a character");
char ch=sc.next().charAt(0);}
}
As above we can get character input and string input.
Today with this article I explained mainly about Java Data Types.With in next article I do hope to discuss about if,else condition.I will go little bit much deeply further step by step.So thank you very much for reading.Keep in touch!