About Java
Java is a high level general purpose programming language which is designed as "write once, use anywhere". This means that it can be run on different platforms without the need to recompile thanks to the Java Virtual Machine ( JVM). This was a revolutionary idea when Java was used back in 1995. Java is used for a variety of purposes including websites, desktop applications and games. Java is free and open source.
Input and Output
To display the text "Hello World use:
System.out.println("Hello World");
To get input from the keyboard use the Scanner class:
1) Import the Scanner library:
import java.util.Scanner;
2) create a Scanner object:
Scanner myScanner= new Scanner(System.in);
System.out.println("Enter your name");
3) use the nextLine member method from the Scanner class:
String Name = myScanner.nextLine();
System.out.println("Your name is: " + Name );
Note: Instead of nextLine() you can use methods more specific to the data type expected. For example if your user is about to type an integer use nextInt().
System.out.println("Hello World");
To get input from the keyboard use the Scanner class:
1) Import the Scanner library:
import java.util.Scanner;
2) create a Scanner object:
Scanner myScanner= new Scanner(System.in);
System.out.println("Enter your name");
3) use the nextLine member method from the Scanner class:
String Name = myScanner.nextLine();
System.out.println("Your name is: " + Name );
Note: Instead of nextLine() you can use methods more specific to the data type expected. For example if your user is about to type an integer use nextInt().
Converting between Data Types
To convert an integer to a string use the valueOf method:
String s=String.valueOf(9);
String s2=String.valueOf(8);
System.out.println(s+s2); // will display 98 as the string values will be concatenated.
String s=String.valueOf(9);
String s2=String.valueOf(8);
System.out.println(s+s2); // will display 98 as the string values will be concatenated.
There are several methods to convert numbers to strings. Check the following websites:
To convert a string to an integer use:the parseInt method of the Integer class:
int n = Integer.parseInt("9");
int n2 = Integer.parseInt("8");
System.out.println(n+n2); // will add the two integer values and display the result which is 17.
int n = Integer.parseInt("9");
int n2 = Integer.parseInt("8");
System.out.println(n+n2); // will add the two integer values and display the result which is 17.