Followers

Thursday, 26 October 2017

CORE JAVA CONCEPTUAL PROGRAMS (For Reference only)

****************************************CORE JAVA BASIC PROGRAMS**********************************

1.HELLO WORLD in Java - hello.java

public class hello {
public static void main(String[] args) {
System.out.println("Hello World"); //print Hello World
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. PRIMITIVE DATATYPES - variables.java

public class variables {
public static void main(String[] args) {
int myNumber=12;
short myShort=234;
long myLong=78788;
double myDouble= 7.5656;
float myFloat=6878.7f;
char myChar='S';
boolean myBoolean1= false;
boolean myBoolean2=true;
byte myByte=127;

System.out.println(myNumber);
System.out.println(myShort);
System.out.println(myLong);
System.out.println(myDouble);
System.out.println(myFloat);
System.out.println(myChar);
System.out.println(myBoolean1);
System.out.println(myBoolean2);
System.out.println(myByte);
}
}

OUTPUT : 
12
234
78788
7.5656
6878.7
S
false
true
127

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.STRING DATATYPE AND THEIR CONCATENATION -strings.java

public class strings {

public static void main(String[] args) {
String myString="Hello....";
System.out.println(myString);

String s1="Sonali";
String day="Monday";
System.out.println(myString+" My name is "+s1+" ...Today is "+day);
}
}

OUTPUT :

Hello....

Hello.... My name is Sonali ...Today is Monday

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4.WHILE LOOP  - loop.java

public class loop {

public static void main(String[] args) {
int i = 0;

while (i < 10) {
System.out.println("Loop " + i);
i++;
}
}
}

OUTPUT :

Loop 0
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
Loop 7
Loop 8

Loop 9

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

5.FOR LOOP

public class forLoop {

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.printf("This is %d th loop...\n", i);
}
}
}

OUTPUT :

This is 0 th loop...
This is 1 th loop...
This is 2 th loop...
This is 3 th loop...
This is 4 th loop...
This is 5 th loop...
This is 6 th loop...
This is 7 th loop..
This is 9 th loop...

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

6.IF LOOP : ifLoop.java

public class ifLoop {

public static void main(String[] args) {
if(2<5){
System.out.println("Second number is greater...");
}
}

}

OUTPUT :

Second number is greater...

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

7.GETTER USER INPUT : inputUser.java

import java.util.Scanner;

public class inputUser {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter a text input");
String text = sc.nextLine();
System.out.println("Entered value is : " + text);

System.out.println("Enter an integer value");
int intvalue = sc.nextInt();
System.out.println("Entered integer value is : " + intvalue);

System.out.println("Enter an float value");
float floatvalue = sc.nextFloat();
System.out.println("Entered float value is : " + floatvalue);

System.out.println("Enter an double value");
double doublevalue = sc.nextDouble();
System.out.println("Entered double value is : " + doublevalue);

}


}

OUTPUT :

Enter a text input
hello
Entered value is : hello
Enter an integer value
3
Entered integer value is : 3
Enter an float value
4.5
Entered float value is : 4.5
Enter an double value
2324.4

Entered double value is : 2324.4


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------