Data Types in java
There are three
main data types for java
1.Integer
2.String
3.Floating point
Integer
It used to store
number
ie. int a; "declaration of variable a"
int a=2;
"initialize value 2 for variable a;
There are four
types of integer
TYPE SIZE
byte one byte
short two byte
int four byte
long eight byte
• To make the integer long L or 1 is require at the
end of number
623L or 6231
Example
class integers
{
public static void
main(String[] args)
{
byte b=22;
short s=1234;
int a=1324356;
long j=12345676543L
;
System.out.println("b=
"+b);
System.out.println("s=
"+s);
System.out.println("a=
"+a);
System.out.println("j=
"+b);
}
}
In above example we
take all the types of integers
and print their
value
String
It used to store
sequence of character
creation of String
ie. String
name=""; //Empty string
String name="Alex" //character
series.
Substring
We can take small
String (Substring) from larger String
in this method
String Welcome="Helloworld"
String z=greeting substring (0,3) //we take the
character from 0 up to <3;
Means we copy 0,1,2
from position 0 to position 2
Here we get
substring s with characters “Hel”
Calculate the
length of substring
Suppose we have the
substring
String z=greeting substring (Y,Z);
Then string
length=Z-Y
String z=greeting substring (0,3)
Length for above
substring=3-0=3
Concatenation of strings
Concatenation is
use for joining of two string with
+ sing
Example
class concatenation
{
public static void
main(String[] args)
{
String one="Hello";
String two="World";
String three=one +
two; //joining of two string
System.out.println(three);
}
}
In above program we
get output “HelloWorld”
+ sing join two
string in order they are arrive
Floating point
It store the number
containing fractional parts
Ie. Float a=33.40,b=22.12;
Floating point type
TYPE SIZE
float 4 byte(approximately 7 decimal digit)
double 8 byte(approximately 15 decimal digit)
Floating point
number have suffix F(for example 2.33F)
And the floating
point number without an F suffix (such as 2.33)
Is conceder as of
type double
Double is the type of floating point.
For mathematical
function such as sin, cos double is require
Example:
class type
{
public static void
main (String[] args )
{
float a=3.14F;
double
b=0.0000000332;
System.out.println("a="+a);
System.out.println("b="+b);
}
}
In above program we
print the value of a and b
No comments:
Post a Comment