these posts are going lengthy, so I will try to make them as short as I can.
next topic:
Fundamental Concepts
Date Types:
1. Integer :- used to store integer values. four types: byte(1 byte), short(2 bytes), int(4 bytes), long(8 bytes)
2. Floating Point :- used to store fractional numbers. two types: float(4 bytes) and double(8 bytes)
3. Character :- to store characters. char(2 bytes)
4. Boolean :- to store logical values true and false only.
Literals:
A literal is a constant value that does not have a identifier. commonly used to initialize variables. they can be number, character, string or boolean value. for example: 23, 43.54, true, 'f' etc.
types:
1.
Integer literals:- represent integer values. values can be in decimal(base 10), Octal(base 8) or hexadecimal(base 16) form.
note: with octal and hexadecimal literals prefixes 0 and 0x are used respectively.
2.
Floating Point literals:- represent fractional values.
note: by default all floating point literals have double data type. that's why error will occur if we assign a floating point literal to a variable of float data type because automatic type casting is not possible here(learn about it later). to avoid this error we have to put f or F at the end of literal to make it float. for example: float a=5.34f;
3.
Boolean Literals:- represent true or false only.
note: they cannot be represented with 0 or 1 values like C and C++.
4.
Character literals:- represent single character or an escape sequence character. for example: 'A', '\n'(for new line) etc.
5.
String literals:- unlike C++, in java string is a class and not a primitive data type. hence all string literals are values of the String class objects.
Type Casting or Type Conversion:
process of converting a variable or value of one date type into another data type.
types:
1.
Automatic type casting:- take place only if the two data types are compatible to each other and the range of the destination data type is longer than the range of the source data type. for example: int and float are compatible to each other.
so an int value can be converted automatically to long value but long value can't be converted automatically to int value because long with 8 bytes is longer than int with 4 bytes.
2.
Explicit type casting:- for the above explained problem in automatic type casting we have to do explicit type casting.
syntax: target_variable = (target_datatype) source_variable;
Operators:
operators allow programmer to perform certain operations on data and variables also called as operands.
types:
-Arithmetic: +, -, *, / and %
-Assignment: = is simple assignment and +=, -=, *=, /=, %= are compound assignments. for example: i += 10 is same as i = i + 10
-Relational: ==, !=, <, >, <= and >= are used to determine relationship between two operands and always return a boolean value.
-Logical: &, |, ^, ||, &&, !, &=, |=, ^=, ==, != operate only on boolean operands.
-Increment and Decrement: ++ and -- can be used as prefix and postfix of an operand. In prefix the value of the operand is incremented or decremented before assigning it to another variable and in postfix after.
-Bitwise: &, |, ^, >>, << perform on individual bits and are applied to only integer types(byte, short, int and long).
-special: instanceOf and dot operators. instanceOf check whether or not an object is of a particular type(class, interface or array) and dot(.) is used to invoke methods in a class.
operator precedence:
The operator with higher level of precedence is evaluated first when more than one operators are used in an expression.
here is the list with decreasing operator precedence:
- (), []
- unary minus, ++, --, !
- *, /, %
- +, -
- <<, >>
- <, <=, >, >=, instanceOf
- ==, !=
- &
- ^
- |
- &&
- ||
- =
Arrays in Java:
array is a collection of storage locations for same type of data.
concept is same as C, C++.
only
difference is that, in java declaring an array does not mean memory is allocated.
syntax: data_type var_name[];
new operator is used to allocate memory to an array.
syntax: var_name = new data_type [size];
we can also combine declaration and creation.
syntax: data_type var_name[] = new data_type[size];
we can also declare, create and initialize array at the same time.
syntax: data_type var_name[] = {array_element1, array_element2, array_elementn};
also JVM automatically creates an array with size equal to the total number of elements added.
enjoy learning🎉