Basic Concepts
The previous topic cleared the compiler and interpreter doubt but still, there are questions such as “How to store different numbers in java?” or “What value you can store in java and how?”, etc.
To clear all such doubts we have prepared this “Language Fundamentals” topic which includes the basic concepts to write programs in Java.
Watch this Java video by Intellipaat:
Keywords
- Keywords are the reserved words which are having predefined meaning in Java.
- All the keywords are defined in lower case and the meaning of these keywords can’t be modified.
- We cannot use keywords as names for variables, classes, methods, or as any other identifiers.
- const & goto are the keywords but no implementation available in Java. You cannot use in Java.
- By the name, it is clear that identifiers are used for identification purpose.
- Identifiers can be a class name, method name, variable name, or a label name. For Example:
class TestIntellipaat{
public static void main(String args[]){
int a=2;
}
}
In the above example,
-
- TestIntellipaatis a class name.
- main is a method name.
- String is a predefinedclass name.
- args and a is a variable name.
Rules for defining identifiers:
- Identifier can contain alphabets [A-Z] & [a-z], Digits [0-9], underscore(_) and dollar($).
- The first letter must be an alphabet, digit, underscore or dollar sign.
- Space is not allowed in between the identifier name.
- Keywords cannot be used as identifiers. For example:
Valid Identifiers: ch, _integer, del123, deal_var, etc.
Invalid Identifiers: 12ch, @charm, My var, a+6, while, int, true, etc.
Majority of Java Developers are moving towards Hadoop? Want to know why? Check this Reasons You Should Switch Career From Java to Hadoop blog!
Data Types
Data types represent the type of data you want to use and memory required for that data.
There are two types of data:
- Primitive Data Type
- Non-Primitive Data Type
Primitive Data Types
These are predefined data types. There are eight types of primitive data types.
Primitive Data Types |
Default Values |
Range |
Default
Size |
Example |
boolean |
false |
true/ false |
N/D |
boolean b = true |
char |
0 or
\u0000 |
0 to 65535 (2^16-1)
\u0000 to \uffff |
2byte |
char c=’A’ |
byte |
0 |
-128(-2^7) to 127 (2^7 -1) |
1byte |
byte b = 12 |
short |
0 |
-32,768 (-2^15) to 32,767(2^15 -1) |
2byte |
short s=10 |
int |
0 |
– 2,147,483,648 (-2^31) to2147483647(2^31 -1) |
2byte |
int I = 100 |
long |
0 |
-2^63 to 2^63-1 |
8byte |
long l= 1000L |
float |
0.0 |
1.4E-45 to 3.40E+38 |
4byte |
float f = 25.9f |
double |
0.0 |
4.9E-324 to 1.79E+308 |
8byte |
double d = 152.3 |
Any
Reference
type |
null |
Reference of the corresponding type object |
8byte |
String str=null |
Non-Primitive Data Types:
These are the user-defined data types. There are four types of non-primitive data types:
- Class type
- Interface type
- Enum type
- Annotation type
Watch this Java Interview Questions video by Intellipaat:
Variables
Variable holds the user data. The memory will be allocated for the variables according to the data type. Value of the variable can be changed any number of times during the program execution. Syntax: <data type><var name>; or <data type><var name>=<value>;
Example: int a; int b=10; String s=”name”; String st; There are two types of variables based on the data types used to declare the variable:
- Primitive variables
- Reference Variable
Primitive Variables
Variables declared with primitive data types are called as primitive variables. Example: int a, char c, double b=10.0, etc.
Read our comprehensive guide to the Final Keyword in Java and become a Java master!
Reference Variable
Variables declared with used defined data types are called as reference variables. Example:String str; String s=”Intellipaat”;There are following types of variables based on the scope of the variables:
- Instance variables
- Static variables
- Local variables
public class IntellipaatClass{
int a; // Instance Variable
static int b; // static variable
void show(){
int c=10; // local variable
}
}
Instance Variables
Variables declared without using static keyword inside of a class but outside of method is called instance variables. Example: variable a in the above code. The Memory will be allocated to the instance variable only at the time of object creation. As many times as the object is created, the instance variable will get the memory.
Static Variables
Variables declared using static keyword inside of a class but outside of method are called static variables. Example: variable b in the above code. The Memory will be allocated to static variables at the time of object creation when the class loads. Static Variable will get the memory only once when the class loads.
Local Variables
Variables declared inside of a method are called as Local variables. Example: variable c in the above code.
Constants
Constants are the special variables whose value can’t be modified during the program execution. These are also called final variables. Example:final int a=99; final String s=”Intellipaat”; final double d=10.1;
Know the major differences between today’s most prominent languages in the IT industry, check out this Java vs Python blog!
Get 100% Hike!
Master Most in Demand Skills Now !
Literals
Literals are the actual values that are assigned to variables or constants. There are six types of literals:
- Boolean literals
- Character literals
- String literals
- Integral literals
- Floating literals
- null literal
Escape Sequence
Escape Sequence is a special notation which is used to represent some special characters which can’t be represented as it is.
Escape Sequence |
Description |
\t |
Tab space |
\b |
backspace |
\n |
newline |
\r |
Carriage return |
\f |
formfeed |
\’ |
Single quote character |
\” |
Double quote character |
\\ |
backslash |
ASCII Character Sets
- ASCII stands for American Standard Code for Information Exchange.
- Every character enclosed in a single quotation mark will have an integer equivalent value called as ASCII value. Range of ASCII value is 0-255.
UNICODE Character Sets
- UNICODE is the Unicode Worldwide Character Standard. It contains many diverse characters. Each character must have a UNICODE value.
- Syntax: \uXXXX, where X will be a hexadecimal number.
- Range: 0 to 65535
Why UNICODE?
Every country has its own character representation in an integer value. United States has ASCII, Europe has ISO 8859-1, Russia has KOI-8, etc. So, it was thought to create a character set that contains all the characters. There UNICODE came into play.
Learn about the most trending programming model for Big Data, in this very informative tutorial blog on Java Mapreduce Tutorial!
OPERATORS
Operators are the symbols that perform the operation on values. These values are known as operands. There are three types of operators depending on the number of operands required:
- Unary Operator: Only one operand is required.
- Binary Operator: Two operands required.
- Ternary Operator: Three operands required.
Following are the types of operator depending on the operation:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Conditional Operator
Arithmetic Operator
- Arithmetic Operators are used to performing an arithmetic operation on the operands.
- Operands used in this can be of the numeric type or string type. Result of the arithmetic operator is always int or greater than int.
Operator
|
Operator Name |
Description |
Example
|
+ |
Addition |
Adds two operands or do string concatenation. |
a=1, b= 2
a + b = 3
OR
String str=”Hello”;
str+10 = Hello10 |
– |
Subtraction |
Subtracts |
a=1,b= 2
b – a = 1 |
* |
Multiplication |
Multiplies |
a=1,b= 2
a * b = 2 |
/ |
Divide |
Perform division operation |
a=1,b= 2
b / a = 2 |
% |
Modulus |
Returns remainder |
a=1,b= 3
b % a = 1 |
++ |
Increment |
First checks the condition, then Increase the operand value by 1 |
a=1
a++;
a value becomes 2 |
— |
Decrement |
First checks the condition, Decrease the operand value by 1 |
a=2
a–;
a value becomes 1 |
Relational Operator
- Relational operators are used to check the relation between the two operands.
- Result of the relational operator is always Boolean value.
Here is the list of relational operators:
Operator |
Operator Name |
Description |
Example |
== |
Equal to |
Returns true if two operands are equal, otherwise returns false |
a=2, b=2
if(a== b)
result= true |
!= |
Not Equal to |
Returns true if two operands are not equal, otherwise returns false |
a=2, b=2
if(a== b)
result=false |
< |
Less than |
Checks the lesser value between the two operands. |
a=3, b=2
if(a<b)
result=false |
> |
Greater than |
Checks the greater value between the two operands. |
a=3, b=2
if(a>b)
result= true |
<= |
Less than or equal to |
If the value of left operand is less than or equal to the value of right operand then it returns true. |
a=23, b=2
if(a<= b)
result= false |
>= |
Greater than or equal to |
If the value of left operand is greater than or equal to the value of right operand then it returns true. |
a=22, b=2
if(a>= b)
result= true |
Logical Operator
- Logical Operators are used to perform logical operations.
- Result of this operator is always a Boolean value.
&& |
Logical AND |
When Both conditions are true, the result is true
otherwise false |
|| |
Logical OR |
When at least one condition is true,
then the result is true otherwise false |
! |
Logical NOT |
Reverse the condition
!true= false
!false= true |
Assignment Operator
- Assignment Operators are used to assign values to the operand.
Operator |
Operator Name |
Description |
Example |
= |
Assignment |
It assigns value to left-hand side operand |
a = 2
It assigns 2 to a |
+= |
Add then assign |
It performs addition and then result is assigned to left-hand operand |
a+=b means
a = a + b |
-= |
Subtract then assign |
It performs subtraction and then result is assigned to left-hand operand |
a-=b means
a = a – b |
*= |
Multiply then assign |
It performs multiplication and then result is assigned to left-hand operand. |
a*=b means a = a * b |
/= |
Divide then assign |
It performs division and then result is assigned to left-hand operand |
a/=b means
a = a / b |
%= |
Modulus then assign |
It performs modulus and then result is assigned to left-hand operand |
a%=b means
a = a % b |
Learn Java from scratch with this free Java Tutorial!
Bitwise Operators
- Bitwise Operators are used to perform operations on individual bits.
<<= |
Left shift AND assignment operator |
It performs Binary left shift and then result is assigned to left-hand operand |
a=5; a>>=7 means 7 times left shift, then result to a |
>>= |
Right shift AND assignment operator |
It performs Binary right shift and then result is assigned to left-hand operand |
a=5; a>>=7 means 7 times right shift, then result to a |
&= |
Bitwise AND assignment operator |
It performs bitwise AND then result is assigned to left-hand operand |
a=5; a&=7 means bitwise AND operation, then result to a |
^= |
bitwise exclusive OR
and assignment operator |
It performs bitwise exclusive OR and then result is assigned to left-hand operand |
a=5; a^=7 means bitwise XOR operation, then result to a |
<< |
Left shift operator |
It performs Binary left shift |
a<<1 means one bit is left-shifted |
>> |
Right shiftoperator |
It performs Binary right shift |
a>>1 means one bit is right-shifted |
& |
Bitwise AND |
It performs bitwise AND |
5 & 7 means binary XOR of 5 and 7 |
^ |
bitwise exclusive OR |
It performs bitwise exclusive OR |
5^7 means binary XOR operation on 5 and 7 |
| |
bitwise inclusive OR |
It performs bitwise inclusive OR |
2|1 means 2 and 1 binary OR operation |
Conditional Operator
- It is a ternary operator.
Syntax: operand1? operand 2:operand3;
- Both the operand must of Boolean type.
- If true, returns operand2, otherwise returns operand3. Example
boolean I = 3>2?true:false; ->returns true.
new Operator
new operator is used to create objects for class.
Example:
class Test{}
class IntellipaatLearn{
public static void main(String args[]){
Test t=new Test(); // t object is created
}
}
Here, t object will be created in the main memory so that we can access the members of the Test class.
instanceof Operator
It is used to check whether the given object belongs to a specified class or not. It is also called Type Comparision Operator. It returns a Boolean value.
Syntax: <reference variable>instanceof<classname>
Example:
Class Test{}
class IntellipaatLearn{
public static void main(String args[]){
Test t=new Tets(); // t object is createdSystem.out.println(t instanceof Object); // returns true
}
}
Output:
true
Interested in learning Java? Enrol in our Java online course now!
Operators Precedence
Operator precedence means the priorities of operators.
Say you have given an equation 10-2*2. How will you know whether to subtract first or multiply? To solve this confusion, operator precedence has provided.
First, 2*2 will be performed according to the below table then, the result will be multiplied by 10. The answer will be 6.
Category |
Operator |
Associativity |
Postfix |
() [] . (dot operator) |
Left to right |
Unary |
++ – – ! ~ |
Right to left |
Multiplicative |
* / % |
Left to right |
Additive |
+ – |
Left to right |
Shift |
>>>>><< |
Left to right |
Relational |
>>= <<= |
Left to right |
Equality |
== != |
Left to right |
Bitwise AND |
& |
Left to right |
Bitwise XOR |
^ |
Left to right |
Bitwise OR |
| |
Left to right |
Logical AND |
&& |
Left to right |
Logical OR |
|| |
Left to right |
Conditional |
?: |
Right to left |
Assignment |
= += -= *= /= %=>>=<<= &= ^= |= |
Right to left |
Comma |
, |
Left to right |