🎏
Print 📍
Fo sho, hello world! 📍
- Print a specified string and appends a newline character at the end.
int a = 0;
System.out.println("The value of a is " + a);
- Print a specified string (without a
\n
at the end).
System.out.print();
- Print a specified string with variables that needs to be formatting using format specifiers (without a
\n
at the end).
String item_name = "Golden Apple";
int amount = 12;
System.out.printf("We got %d %s in the barrel!", amount, item_name);
- Print a single character at a time (without a
\n
at the end) | Optional
char c = 'a';
System.out.write(c);
- Logging framework to print log messages to the console | Optional
import java.util.logging.Logger;
Logger logger = Logger.getLogger("MyLogger");
logger.info("[INFO] Welcome to my program!");
- Print a specified string as a error message and appends a newline character at the end | Optional
System.err.println("404 NOT FOUND"); // Just an example
Short-cut for System.out.println()
on VS Code 📍
- Type
sysout
and press tab
Printf specifiers 📍
Specifier | Data Type |
---|---|
%d |
int |
%s |
String |
%c |
char |
%f |
float, double |
%e |
Scientific notation, float or double |
%o |
Octal number |
%x |
Hexadecimal number |
%% |
Preserve percentage sign |
%n |
New line |
Additional Printf specifiers - int 📍
int population = 8500000; // EXAMPLE
Specifier | Output | Explain |
---|---|---|
%,12d |
8,500,000 |
12 spaces in the String, will leave spaces at front if possible, includes grouping separators |
%+,12d |
+8,500,000 |
Add a positive sign + at front, 12 spaces in the String, will leave spaces at front if possible |
%-+,12d |
+8,500,000 |
Add a positive sign + at front, 12 spaces in the String, will leave spaces at end if possible |
%012d |
000008500000 |
12 spaces in the String, will leave 0 at front if possible |
%o |
40331440 |
Octal |
%x |
81b320 |
Hexadecimal |
Additional Printf specifiers - float 📍
double pearls_sell = 15.2; // EXAMPLE, in Java, mostly use double type instead of float
Specifier | Output | Explain |
---|---|---|
%.4f |
15.2000 |
4 decimal places |
Additional Printf specifiers - String 📍
String name = "dango" // EXAMPLE
Specifier | Output | Explain |
---|---|---|
%7s |
dango |
7 spaces in the String, will leave spaces at front if possible |
%-7s |
dango |
7 spaces in the String, will leave spaces at end if possible |
%-7S |
DANGO |
S will uppercase dango |
As you may notice that some specifiers work in all data types, for instance, %2d
, %2f
, %2s
, even %-2d
.
GOOD PRACTICE
Always preserve a newline at the end of a program.
Declaration & Memory Allocation 📍
Sizes of Common Data Types 📍
Data Type | Size in Bytes |
---|---|
char | 1 |
short | 2 |
int | 4 |
long | 8 |
float | 4 |
double | 8 |
bool | 1 |
- A String is made up of multiple characters.
Java Primitive Type Data Range 📍
short 📍
- Signed
$16$ bits -
$-32768$ to$32767$
int 📍
- Signed
$32$ bits -
$-2147483648$ ~$2147483647$
long 📍
- Signed
$64$ bits -
$-9223372036854775808$ ~$9223372036854775807$
float 📍
-
$32$ bits -
$1.40239846\mathrm{e}{-45}f$ ~$3.40282347\mathrm{e}{+38}f$
double 📍
-
$64$ bits -
$4.94065645841246544\mathrm{e}{-324}$ ~$1.79769313486231570\mathrm{e}{+308}$
char 📍
- Unsigned
$16$ bits -
$0('\backslash u 0000')$ ~$65535 ('\backslash uffff')$
byte 📍
- Signed
$8$ bits -
$-128$ ~$127$
void 📍
- n/a
- n/a
Science Notation 📍
For example, 1.5e+3
in maths will be 1500
How about 1.5e-3
, it'll be 0.0015
Initialisation (in details) 📍
Look at the following code snippet:
int min; // Declare an integer variable without initialising
int max = 0; // Set a default integer value to 0
min
variable is created but not initialised, which means that the vlaue it contains is undefined. That is, the actual value of min
could be any arbitrary value that happened to be stored in that memory location.
Well on the other hand, max
is created and has a well-defined initial value.
In both cases, on most systems, an int
typically occupies 4 bytes of memory. The memory allocated for min
and max
are typically located on the stack (local variables) or the heap (dynamically allocated variables).
GOOD PRACTICE
It's worth noting for declareing a variable without being assigned to a value.
Input 📍
Library 📍
import java.util.Scanner;
Create a Scanner object 📍
Scanner scnr = new Scanner(System.in);
The scnr
represents as an instance of the Scanner
class.
Methods 📍
scnr.nextInt()
, integer inputscnr.nextDouble()
, double inputscnr.next()
, string input but in the same linescnr.nextLine()
, string input for the whole linescnr.next().charAt(0)
, char input
GOOD PRACTICE
scnr.close();
To release system resources as soon as you're finished with them to ensure efficient resource utilization and prevent any potential issues.
Maths 📍
Library 📍
import java.lang.Math;
Commonly Used Methods 📍
Math.log()
, natural logarithmMath.sqrt()
, square rootMath.sin()
, sineMath.cos()
, cosineMath.tan()
, tangentMath.pow()
, powerMath.abs()
, absolute valueMath.PI
, the value of πMath.E
, the value of eMath.floor()
, round downMath.ceil()
, round up
Reminder 📍
- Ignore good practices only when doing challenges.
- Understand the question step by step.
- Quickly come up with a solution and try it out, even if it might be wrong. (Hopefully not)
- Keep debugging and practicing.