Difference between revisions of "Java"

From RHS Wiki
Jump to navigation Jump to search
Line 54: Line 54:
 
for(int i=0; i<5;i++){
 
for(int i=0; i<5;i++){
 
System.out.printf("The value of i is %d\n", i);
 
System.out.printf("The value of i is %d\n", i);
 +
}</source>
 +
== Conditional ==
 +
=== if ===
 +
<source lang="java">
 +
int value = 20;
 +
 +
if(value < 10){
 +
System.out.println("Value < 10");
 +
}
 +
else if(value >= 10 && value < 20){
 +
System.out.println("value >=10 and value < 20");
 +
}
 +
else {
 +
System.out.println("value >= 20");
 
}</source>
 
}</source>

Revision as of 04:02, 26 March 2015

hello world

public class aplication {

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

	}

}

variables

public class variables {

	public static void main(String[] args) {
		int myInt = 88; //Integer 32 bit
                short myShort; //Short 16 bit
                long myLong; //Long 64 bit
                byte myByte; //Byte 8 bit

                double myDouble; //Double 
                float myFloat = 325.25f //Float
                
		char myChar = 'y'; //Char
                boolean myBoolean = true; //Boolean
	}

}

Strings

public class stringstuto {
	public static void main(String[] args) {
                int myInt = 7;
		String myString1 = "Hello ";
                String myString2 = "Rafa";
                String myString3 = myString1 + myString2;
                
                System.out.println(myString3);
                System.out.println("My integer is: " + myInt);
	}
}

Loops

While

int myInt = 0; 
	
while(myInt < 10){
	System.out.println("Hello");
	myInt = myInt + 1;
}

For

for(int i=0; i<5;i++){
	System.out.printf("The value of i is %d\n", i);
}

Conditional

if

int value = 20;
		
if(value < 10){
	System.out.println("Value < 10");
}
else if(value >= 10 && value < 20){
	System.out.println("value >=10 and value < 20");
}
else {
	System.out.println("value >= 20");
}