System is the object for input and output (i/o).
public class example_print_values {
public static void main(String[] args) {
System.out.print("Hello World without a new line. ");
System.out.println("Hello World with a new line at the end");
System.out.printf("Hello %s", "World"); //Print with formatting(use replacements)
}
}
Sample 1.1-1: example_print_values.java
The compile and run the program above:
javac example_print_values.java
java example_print_value
Hello World without a new line. Hello World with a new line at the end.
Hello World
In Sample 1.1-1, the 1st line defines the class. The filename must match the class name
public class example_print_values is a class. The public static void main(String[] args) is a function. The code inside functions and classes have to be surrounded by curly braces.
A class cannot be executed by it self. A class has to have a main method to be executed
Each statement has to be ended with a semi-colon
The main method has 1 parameter called args. It is an array of strings.
Comments are ignored by the compiler. //print with formatting(have multiple parameters) is a comment.
A Scanner object is a way to get input from a user. It is part of the Scanner library. The library is imported before the class.
import java.util.Scanner;
You have to import it.
public class input_example{
public static void main(String[] args) {
System.out.println("Enter a number: ");
Scanner numInput = new Scanner(System.in);
double num = numInput.nextDouble();
System.out.printf("You Entered %d", num);
}
}
Sample 1.1-2: input_example
Enter a number: 59.8
You Entered 59.8
System.in means to get input on the console.
Double is a data type. It means a number with any number of decimals and nextDouble() converts input into a double.
Scanner is a class, numInput is an object.
%d is an format. d stands for double. We used %d because num is a double.
Operators are mathematical operations acted on one or more numbers or numerical variables
Assignments are when variables are initialized with values. Doubles, floats, longs, bytes, shorts, and integers are numbers.
public class number_example{
public static void main(String[] args) {
double a = 3.248735647390237;
int b = 1234567890;
long c = 34592034975984L;
float d = 24.235235F;
byte e = -46;
short f = 423;
boolean g = false;
char h = 'h';
String i = "Hello world";
System.out.println("Double: " + a);
System.out.println("Integer: " + b);
System.out.println("Long: " + c);
System.out.println("Float: " + d);
System.out.println("Byte: " + e);
System.out.println("Short: " + f);
System.out.println("Boolean: " + g);
System.out.println("Char: " + h);
System.out.println("String: " + i);
}
}
Sample 1.1-3: number_example.java
Double: 3.423452678000000
Integer: 5784212
Long: 235857921312344
Float: 24.235235
Byte: -46
Short: 423
Boolean: false
Char: h
String: Hello World
Doubles are decimal numbers with any amount of decimals. If the number of decimals is less than 15, extra zeros are added.
Integers are whole numbers with any amount of digits.
Longs are whole numbers with 16 digits. They must have an L suffix.
Floats are decimal numbers with 6 decimals. They must have an F suffix.
Bytes are numbers with 3 or less digits.
Shorts are numbers with 5 or less digits.
Booleans are either true or false.
Char is a string with only 1 character
Strings are text.
import java.util.Random;
public class random_example {
public static void main(String[] args) {
Random randomGenerator = new Random();
int randNum = randomGenerator.nextInt(10);
int randNum2 = randomGenerator.nextInt(10, 20);
double randNum3 = randomGenerator.nextDouble(10, 20);
System.out.println(randNum);
System.out.println(randNum2);
System.out.println(randNum3);
}
}