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.
Java1
Here are the topics if you directly want to jump into:

Watch this Java video by Intellipaat:

Java Fundamentals 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

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.

java

Identifiers

  • 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 7 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

java
Primitive Data Types
These are predefined data types. There are eight types of primitive data types.

Primitive Data TypesDefault ValuesRangeDefault
Size
Example
booleanfalsetrue/ falseN/Dboolean b = true
char0   or
\u0000
0   to   65535 (2^16-1)
\u0000 to \uffff
2bytechar c=’A’
byte0-128(-2^7) to 127 (2^7 -1)1bytebyte b = 12
short0-32,768 (-2^15)  to  32,767(2^15 -1)2byteshort s=10
int0– 2,147,483,648 (-2^31)  to2147483647(2^31 -1)2byteint I = 100
long0-2^63   to   2^63-18bytelong l= 1000L
float0.01.4E-45   to   3.40E+384bytefloat f = 25.9f
double0.04.9E-324   to   1.79E+3088bytedouble d = 152.3
Any
Reference
type
nullReference of the corresponding type object8byteString 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:

Java Fundamentals 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

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.
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!

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 SequenceDescription
\tTab space
\bbackspace
\nnewline
\rCarriage return
\fformfeed
\’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!

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 NameDescription

Example

+AdditionAdds two operands or do string concatenation.a=1, b= 2
a + b = 3
OR
String str=”Hello”;
str+10 = Hello10
SubtractionSubtractsa=1,b= 2
b – a = 1
*MultiplicationMultipliesa=1,b= 2
a * b = 2
/DividePerform division operationa=1,b= 2
b / a = 2
%ModulusReturns remaindera=1,b= 3
b % a = 1
++IncrementFirst checks the condition, then Increase the operand value by 1a=1
a++;
a value becomes 2
DecrementFirst checks the condition, Decrease the operand value by 1a=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:

OperatorOperator NameDescriptionExample
==Equal toReturns true if two operands are equal, otherwise returns falsea=2, b=2
if(a== b)
result= true
!= Not Equal toReturns true if two operands are not equal, otherwise returns falsea=2, b=2
if(a== b)
result=false
<Less thanChecks the lesser value between the two operands.a=3, b=2
if(a<b)
result=false
>Greater thanChecks the greater value between the two operands.a=3, b=2
if(a>b)
result= true
<=Less than or equal toIf 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 toIf 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 ANDWhen Both conditions are true, the result is true
otherwise false
||Logical ORWhen at least one condition is true,
then the result is true otherwise false
!Logical NOTReverse the condition
!true= false
!false= true


Assignment Operator

  • Assignment Operators are used to assign values to the operand.
OperatorOperator NameDescriptionExample
=AssignmentIt assigns value to left-hand side operanda = 2
It assigns 2 to a
+=Add then assignIt performs addition and then result is assigned to left-hand operanda+=b means
a = a + b
-=Subtract then assignIt performs subtraction and then result is assigned to left-hand operanda-=b means
a = a – b
*=Multiply then assignIt performs multiplication and then result is assigned to left-hand operand.a*=b means a = a * b
/=Divide then assignIt performs division and then result is assigned to left-hand operanda/=b means
a = a / b
%=Modulus then assignIt performs modulus and then result is assigned to left-hand operanda%=b means
a = a % b

Bitwise Operators

  • Bitwise Operators are used to perform operations on individual bits.
<<=Left shift AND assignment operatorIt performs Binary left shift and then result is assigned to left-hand operanda=5; a>>=7 means 7 times left shift, then result to a
>>=Right shift AND assignment operatorIt performs Binary right shift and then result is assigned to left-hand operanda=5; a>>=7 means 7 times right shift, then result to a
&=Bitwise AND assignment operatorIt performs bitwise AND then result is assigned to left-hand operanda=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 operanda=5; a^=7 means bitwise XOR operation, then result to a
<<Left shift operatorIt performs Binary left shifta<<1 means one bit is left-shifted
>>Right shiftoperatorIt performs Binary right shifta>>1 means one bit is right-shifted
&Bitwise ANDIt performs bitwise AND5 & 7 means binary XOR of 5 and 7
^bitwise exclusive ORIt performs bitwise exclusive OR5^7 means binary XOR operation on 5 and 7
|bitwise inclusive ORIt performs bitwise inclusive OR2|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? Enroll in our Java Training 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

 

Recommended Videos

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
7 + 27 =