Java simple program using notepade and cmd-Based on java basic notes
1 Java hello world program
class Hello1{
public static void main(String arg[]){
System.out.println("Hello World");
}
}
2 Java program to create a string variable
class StringExample
{
public static void main(String[] args)
{
String line1 = "Hello world";
String line2 = "welcome to java programming";
System.out.println(line1+'\n'+line2);
}
}
3 Java program to print both small and capital alphabets
class Alphabets {
public static void main(String args[]){
char ch;
System.out.println("Small Alphabets: ");
System.out.println("----------------");
for( ch = 'a' ; ch <= 'z' ; ch++ ){
System.out.print(ch+" ");
}
System.out.println();
System.out.println("Capital Alphabets: ");
System.out.println("----------------");
for( ch = 'A' ; ch <= 'Z' ; ch++ ){
System.out.print(ch+" ");
}
}
}
4 Java program using different data types
class DatatypeExample
{
public static void main(String a[])
{
byte b_value=126;
short s_value=32767;
int i_value=221133;
long l_value=55334422;
float f_value=3.14f;
double d_value=4.888;
char ch='a';
boolean bln=true; //true or false value only
System.out.println("java data type example program");
System.out.println(b_value);
System.out.println(s_value);
System.out.println(i_value);
System.out.println(f_value);
System.out.println(d_value);
System.out.println(b_value);
System.out.println(ch);
System.out.println(bln);
System.out.println("observe the output value carefully");
}
}
5 Java program using instance and static variable-static block
class VariableExample1
{
int pin=207;
String place="vellur";
static int age=20;
static void display()
{
System.out.println("This is static method");
System.out.println("static method is directly accessed");
System.out.println("age :"+age);
}
static
{
System.out.println("\nThis is static block");
System.out.println("static block first accessed\n");
}
public static void main(String a[])
{
VariableExample1 obj=new VariableExample1();
System.out.println(obj.pin);
System.out.println(obj.place);
System.out.println("instance variable accessed through object creation\n");
display();
}}
Comments
Post a Comment