Hey!guys,this is hitesh..and in the second chapter we'll discuss about data types in java and we'll also learn about path setting for java so that we can execute our programs.
Data Types
in Java
1. byte 1 byte
2. short 2 byte
3. int 4 byte
4. long 8 bytes
5. float 4 bytes
6. double 8 bytes
7. boolean undefined (Java Black Book – 2 bytes)
8. char 2 bytes
Note: all are signed
Data inside
the variables
1. Literals
2. Interactive
Input
3. Non-interactive
input
4. File Input
5. Database
input
Literals
The values that we use from our side for some assignment or
expression are called as literals.
Example
int num=6;
double ar=3.14*num*num;
Literals can be of different types
1. Integrals
2. Floatings
3. Characters
4. Strings
5. Booleans
Integrals
By default such numbers take size of int.
Use l or L with long as suffix
int num=6;
long num=6L;
Integral Literals can be of four types
1. Decimal – 0
to 9, default
2. Octal – 0
to 7, starts with 0
3. Hexa – 0 to
9, A to F, Starts with 0x or 0X
4. Binary – 0
and 1, starts with 0b or 0B
Example
//Sample.jar
class First
{
public static void main(String
args[])
{
int x=6789; //decimal
int y=03456; //octal
int z=0X8D5; //hexa
int p=0b101011; //binary
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(p);
}
}
Compile the program using JDK software with JAVAC compiler
provided inside the BIN folder of the JDK
Syntax
JAVAC <programname>
Example
JAVAC Sample.java à
First.class
Now run the class file
JAVA First
Note
If your BIN folder is not set in the PATH the JAVAC and JAVA
will not work.
We need to set the PATH
PATH=C:\Program Files\Java\jdk1.8.0_05\bin
If you want to set the path for one time only use the
following steps
My Computer à Properties à Advance
System Settings à Advanced à
Environmental Variables à System Variables à Path à Edit…
Environmental Variables à System Variables à Path à Edit…
Now paste the PATH after the semicolon
Note
We can also use underscore (_) as number separator
//Sample.java
class First
{
public
static void main(String args[])
{
int
x=12_67_899; //decimal
int
y=07; //octal
int
z=0X8D5; //hexa
int
p=0b10_1011; //binary
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(p);
}
}
Floating Literals
All numbers with decimal point are of double type by default.
Use f or F with float.
double n=6.7;
float x=5.6; //error
float x=5.6f; //correct
Example
class
Second
{
public static void main(String...
args)
{
float x=5.6f;
System.out.println(x);
}
}
Note:
A program name and class name can be same or different but if
a class is public then both must be same.
Character
Literals
Enclosed in single quotes and every character has
corresponding ASCII value
char ch=’A’;
or
char ch=65;
String Literals
Enclosed in double quotes and managed by String class.
String name=”Rohit”;
Now we can use basic string methods on name.
Example
class Third
{
public
static void main(String args[])
{
String
name="Vikas Kumar Singh";
System.out.println("Name
is : "+name);
System.out.println(name.toUpperCase());
System.out.println(name.toLowerCase());
System.out.println(name.length());
}
}
Boolean
Literals
Can have only two values
1. true
2. false
boolean married=true;
That's all about the data types in JAVA
In the next post we'll discuss about taking input from user with buffered reader,and scanner class with some more packages and we'll also learn about Classes and objects.
Thank You
Keep learning :)
Keep Coding ;)
No comments:
Post a Comment