0% found this document useful (0 votes)
90 views

Java Masterclass CodeWithMosh

Primitive types like int and double store simple values and are copied by value, while reference types like classes store complex objects and are copied by reference. Strings are immutable and methods that modify strings return new strings. Arrays allow storing multiple values of the same type and can be initialized with values between braces. Common math functions include rounding, ceil, floor, max, min and random numbers. Input can be read from the console using a Scanner.

Uploaded by

alexgoot
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Java Masterclass CodeWithMosh

Primitive types like int and double store simple values and are copied by value, while reference types like classes store complex objects and are copied by reference. Strings are immutable and methods that modify strings return new strings. Arrays allow storing multiple values of the same type and can be initialized with values between braces. Common math functions include rounding, ceil, floor, max, min and random numbers. Input can be read from the console using a Scanner.

Uploaded by

alexgoot
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

==

Primitive Types vs Reference Types


==

Primitive Types
- byte, short, int, long, float, double, char, boolean
- used for storing simle values
- copied by their value, and these values are independent of each other

Reference Types
- classes eg Array, Date, Point, String
- copied by their references
- used for storing complex objects

==
Strings
==
- immutable // anything that modifies a String returns a new String
eg String name = "Erron";
System.out.println(name.replace("o", "")); // Errn
System.out.println(name); // Erron

String name = "Hello World";


name.length() // 11
name.endsWith("rld") // true
name.indexOf("e") // 1
name.replace(" ", "_") // Hello_World
name.toLowerCase() // hello world
name.toUpperCase // HELLO WORLD
name.trim() // eg " Hello World " => "Hello World"

==
Escape Sequences
==
use Backslash (\) to use special characters
String message = "Hello \"Erron\""; // Hello "Erron"

==
Arrays
==
int[] numbers = new int[2];
number[0] = 1;
number[1] = 2;
System.out.println(numbers.toString());

int[] numbers = {1,2,3};

==
Math
==

Math.round(1.1F) // 1
Math.ceil(1.1) // 2
Math.floor(1.1) // 1
Math.max(1, 2) // 2
Math.min(1, 2) // 1
Math.random() // any number from 0 - 1
Math.random() * 100 // any number from 0 - 100 with decimal value
Math.round(Math.random() * 100) // any number from 0 - 100 without decimal value

==
NumberFormat currency = NumberFormat.getCurrencyInstance();
String result = currency.format(1234.567) // $1,234.57

NumberFormat.getPercetInstance().format(0.1) // 10%

==
Reading Input
==
Scanner scanner = new Scanner(System.in);
System.out.println("Enter age: ");
byte age = scanner.nextByte(); // eg 12
System.out.println("Age: " + age); // Age: 12

==
String[] fruits = {"Apple", "Mango", "Banana"}
for(String fruit : fruits) {
System.out.print(fruit);
}

==

You might also like