How to write basic Java programs & switch case Statement in Java.
Simple Java Program :
Hello Friends,
Today I will give you an example for writing simple core java program.
Code:
public class firstprogram {
public static void main(String args[])
{
System.out.println(“This is my first java program.”);
}
}
Output :
This is my frist java program.
Java program for data type :
Hello Friends,
Today I will give you an example for writing program for different data types in core java.
Code:
public class datatype {
public static void main(String args[])
{
byte a=10;
short b=10*149;
int c =10000*149;
long d=10000*1000*149;
double e=99.9999;
char f=’v’;
boolean g=true;
boolean h=false;
System.out.println(“the value of a=”+a);
System.out.println(“the value of b=”+b);
System.out.println(“the value of c=”+c);
System.out.println(“the value of d=”+d);
System.out.println(“the value of e=”+e);
System.out.println(“the value of f=”+f);
f++;
System.out.println(“the value of f after increment f=”+f);
System.out.println(“the value of g=”+g);
System.out.println(“the value of h=”+h);
}
}
Output :
the value of a=10
the value of b=1490
the value of c=1490000
the value of d=1490000000
the value of e=99.9999
the value of f=v
the value of f after increment f=w
the value of g=true
the value of h=false
Java program for Escape Sequences :
Hello Friends,
Today I will give you an example for writing program for Escape Sequences in core java.
Code:
public class Escapedemo {
public class Escapedemo {
public static void main(String args[])
{
System.out.println(“t This is a tab demo”);
System.out.println(“n This is new line demon”);
System.out.println(“\is printed”);
}
}
Output :
This is a tab demo
This is new line demo
is printed
Java program for do…..while Statement :
Hello Friends,
Today I will give you an example for writing program for “do…while “statement.
Code:
public class dowhiledemo {
public static void main (String args[])
{
int count=1,i=0;
do
{
i=i+1;
System.out.println(“The value of i=”+i);
count++;
}while(count<=8);
}
}
Output :
The value of i=1
The value of i=2
The value of i=3
The value of i=4
The value of i=5
The value of i=6
The value of i=7
The value of i=8
Java Program for switch case Statement :
Hello Friends,
Today I will give you an example for writing program for “Switch case” in core java.
Code:
public class switchcasedemo {
public static void main (String args[])
throws java.io.IOException
{
char choice;
System.out.println(“t Program for switch case demo”);
System.out.println(“What is your sex ?”);
System.out.println(“1.male”);
System.out.println(“2.female”);
System.out.println(“3.None of the above”);
System.out.println(“Enter your choice”);
choice=(char)System.in.read();
switch(choice)
{
case’1′ : System.out.println(“You are Male.”);
break;
case’2′ : System.out.println(“You are female.”);
break;
default : System.out.println(“None of the above. :D”);
}
}
}
Output :
Program for switch case demo
What is your sex ?
1.male
2.female
3.None of the above
Enter your choice