Language Basics
the traditional features of the Java, including variables, arrays, data types, operators and control flow
Variables
kinds of variables in Java
-
Instance Variables(Non-Static Fields)
Instance Variables are unique to each instance of a object(class), objects store their individual states in "non-statis fields"
-
Class Variables(Static Fields)
Class Variables is governed by the object(class), regardless of how many times the class has been instantiated.
-
Local Variables
A method will often store its temporary state in local variables. Local variables are absolutely only visible to the methods in whhich they are declared,
-
Parameters
Primitive Data Types
The Java programming language is statically-typed, which means that all variables must first be declared before they can be used.
Data Type | Default Value(for fields) |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0 |
float | 0.0 |
double | 0.0 |
char | '\u0000' |
boolean | false |
Local variables are slightly different; the compiler never ever never assgin a default value to an unitialized local variable. You musk make sure assign a local variable a value before you attempt to use it or else result in compile-time error
-
Using Underscore Characters in Numeric Literals to Imporve the readability of code.
Place underscores only between digits. you cannot place underscores in the following places;
1. At the beginning or end of a number 2. Adjacent to a decimal point in a floating point literal 3. Prior to an F or L suffix 4. In positions where a string of digits is expected
long creditCardNumber = 1234_6123_2312_2332L; // ✅ float PI = 3.14_15F; //✅ float pi = 3_.1415F; //❌ int x1 = 52_; //❌ int x2 = Ox_52; //❌ int x3 = Ox5_2; //❌
Arrays
An array is a container object that holds a fixed number of values of a single type.The length of an array is established when the array is created.
// Create an array of integers with 10 length
int[] anArray = new int[10];
// Create an array with assigned elements
int[] arrInt = {
100, 200, 300
400, 500, 600
}
Bullet point
- The term "instance variable" is another name for non-static field.
- The term "class variable" is another name for static field.
- A local variable store termporary state; it is declared inside a method.
- A variable declared within the openming and closing parenthesis of a method is called a parameter.
- What are the eight primitive data types supported by the Java programming language? byte,short,int,long,float,double,boolean,char.
- Character strings are represented by the class java.lang.String.
- An array is a container object that holds a fixed number of values of a single value.
Operators
Assignment, Arithmetic, and Unary Operators
- The Arithmetic Operators
The languages provides operators that perform addition, subtraction, multiplication, and division.
Operators | Description |
---|---|
+ | Additive operator(also used for String concatenation) |
- | Subtraction operator |
* | Multiplication operator |
/ | Division operator |
% | Remainder operator |
- The Unary Operators
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression. or inverting the value of a boolean.
Operator | Description |
---|---|
+ | Unary plus operator; indicates positive value |
- | Unary minus operator; negates an expression |
++ | Increment operator; increments a value by 1 |
-- | Decrement operator; decrements a value by 1 |
! | Logical complement operator; inverts the value of a boolean |
- Equality, Relational, and Conditional Operators
Operator | Description |
---|---|
== | equal to |
!= | not equal to |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
-
The conditional Operators
-
&& and || perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operatos exhibit "short-circuiting" behavior.
-
【expressions ? doSth(T) : doSth(F)】as shorthand for an 【if-then-else】
Operator Description && Conditional-And II Conditional-OR ?: Ternary(shorthand for if-then-else statement) -
-
Bitwise and Bit Shift Operators
Operator Description ~ Unary bitwise complement << Signed left shift >> Signed right shift >>> Unsigned right shift & Bitwise And ^ Bitwise exclusive OR I Bitwise inclusive OR