Difference between revisions of "Java"
Jump to navigation
Jump to search
Rafahsolis (talk | contribs) |
Rafahsolis (talk | contribs) |
||
| Line 123: | Line 123: | ||
*Note: Ctrl + Shift + O in eclipse adds all the imports you need. | *Note: Ctrl + Shift + O in eclipse adds all the imports you need. | ||
| − | + | == Arrays == | |
| + | <source lang="java"> | ||
| + | int[] values; | ||
| + | values = new int[3]; | ||
| + | values[0] = 10; | ||
| + | </source> | ||
== Eclipse shortcuts == | == Eclipse shortcuts == | ||
Ctrl + Space -> Autocomplete.<br /> | Ctrl + Space -> Autocomplete.<br /> | ||
Ctrl + d -> Delete Line<br /> | Ctrl + d -> Delete Line<br /> | ||
Ctrl + o -> Automatically add imports<br /> | Ctrl + o -> Automatically add imports<br /> | ||
Revision as of 05:08, 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++;
}
For
for(int i=0; i<5;i++){
System.out.printf("The value of i is %d\n", i);
}
Do While
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int value = 0;
do{
System.out.println("Enter a number: ");
value = myScanner.nextInt();
}
while(value != 5);
System.out.println("5!");
}
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");
}
switch case
switch(VariableIntOrString){
case "start":
System.out.println("Machine started!");
break;
case "stop":
System.out.println("Machine stopped");
break;
default:
System.out.println("Command not recognized");
}
User Input
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner myInput = new Scanner(System.in);
System.out.println("Enter a line of text: ");
String line = myInput.nextLine();
System.out.println("You entered: " + line);
System.out.println("Enter a number: ");
int value = myInput.nextInt();
System.out.println("You entered: " + value);
System.out.println("Enter a number: ");
double dvalue = myInput.nextDouble();
System.out.println("You entered: " + dvalue);
}
}
- Note: Ctrl + Shift + O in eclipse adds all the imports you need.
Arrays
int[] values;
values = new int[3];
values[0] = 10;
Eclipse shortcuts
Ctrl + Space -> Autocomplete.
Ctrl + d -> Delete Line
Ctrl + o -> Automatically add imports