Basics
Basic syntax and functions from the Java programming language.
Boilerplate
Showing Output
It will print something to the output console.
Taking Input
It will take string input from the user
It will take integer input from the user
It will take float input from the user
It will take double input from the user
Primitive Type Variables
The eight primitives defined in Java are int, byte, short, long, float, double, boolean, and char those aren't considered objects and represent raw values.
byte
byte is a primitive data type it only takes up 8 bits of memory.
long
long is another primitive data type related to integers. long takes up 64 bits of memory.
float
We represent basic fractional numbers in Java using the float type. This is a single-precision decimal number. Which means if we get past six decimal points, this number becomes less precise and more of an estimate.
char
Char is a 16-bit integer representing a Unicode-encoded character.
int
int holds a wide range of non-fractional number values.
short
If we want to save memory and byte is too small, we can use short.
Comments
A comment is the code that is not executed by the compiler, and the programmer uses it to keep track of the code.
Single line comment
Multi-line comment
Constants
Constants are like a variable, except that their value never changes during program execution.
Arithmetic Expressions
These are the collection of literals and arithmetic operators.
Addition
It can be used to add two numbers
Subtraction
It can be used to subtract two numbers
Multiplication
It can be used to multiply add two numbers
Division
It can be used to divide two numbers
Modulo Remainder
It returns the remainder of the two numbers after division
Augmented Operators
Addition assignment
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment
Escape Sequences
It is a sequence of characters starting with a backslash, and it doesn't represent itself when used inside string literal.
Tab
It gives a tab space
Backslash
It adds a backslash
Single quote
It adds a single quotation mark
Question mark
It adds a question mark
Carriage return
Inserts a carriage return in the text at this point.
Double quote
It adds a double quotation mark
Type Casting
Type Casting is a process of converting one data type into another
Widening Type Casting
It means converting a lower data type into a higher
Narrowing Type Casting
It means converting a higher data type into a lower
Decision Control Statements
Conditional statements are used to perform operations based on some condition.
if Statement
if-else Statement
if else-if Statement
Ternary Operator
It is shorthand of an if-else statement.
Syntax
Example
Switch Statements
It allows a variable to be tested for equality against a list of values (cases).
Iterative Statements
Iterative statements facilitate programmers to execute any block of code lines repeatedly and can be controlled as per conditions added by the coder.
while Loop
It iterates the block of code as long as a specified condition is True
for Loop
for loop is used to run a block of code several times