Programming Language Terminologies

Data Types In Java:-

 Definition :-

The type of data that we stored in the variable is known as Data Type.

int a=10; // here “int” is data type

char c=’a’; // here “char” is data type

String name=”Sabi”; // here “String” is data type

For example:

According to Data types, languages can be divided into two categories :-

1. Statically Typed Language :

In this type of languages, each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types.

For Example: C, C++, Java, FORTRAN, Pascal, Scala etc

2. Dynamically Typed Languages:

In this type of languages we need not to specify the type of data or variable, it is automatically checked during run-time.

For Example: Objective-C, PHP, Python, JavaScript, Ruby, Lisp etc

 Difference Between Primitive and Non-Primitive Data Types :-

1. The size of Primitive data type is fixed i.e. if we declare , int 10; or int a=1000; in both cases int data type will take 4 byte of memory. But The size of non-primitive data type is not fixed for eg. int[] a=new int[3]; in this case array will take 12 byte of memory but if we declare int[] a=new int[4]; in this case array will take 16 byte of memory, thus size of array is not fixed.

2. Primitive data type are building blocks but non-primitive data types are derived from primitive data types.

 Types Of Data Types :-

There are two types of data types:-

1.Primitive Data Type:-

Primitive types are the most basic data types available within the Java language. The size of all primitive data types are fixed. There are 8 primitive data types which are as follows :

 boolean, byte, char, short, int, float, long, double

2. Non-Primitive Data Types or Derived Data Types :-

Non-Primitive data types are derived from primitive data types. The size of non-primitive data types are not fixed. Following are non-primitive data types :

String, array, class, structures, unions etc

Variables In Java:-

 Definition :-

A variable is the name of memory location that can contain data. A variable thus has a data type.

For example:

int a=10; // here “a” is variable                             

int roll_no=60; // here “roll_no” is variable

char c=’a’; // here “c” is variable

boolean b=true; // here “b” is variable

 Types Of Variables :-

In java variables are of three types :-

  1. Local Variables :
  2. Instance Variable :
  3. Static Variable :

Below is the program which shows all three variables:

class Test

{

int a=10; // here “a” is instance variable

static int b=20; // here “b” is static variable

void add()

{

int c=30, d; // here “c” and “d” are local variables variable

d=a+b+c;

System.out.println(d);

}

void mul()

{

int e=40, f; // here “e” and “f” are local variables variable

f=a*b*c*e; // error as “c” is local variable and cannot be used

              outside add block, so we can write : f=a*b*e;

System.out.println(f);

}

}

Local Variables :-

  1. Declaration:- A local variable is declared inside the body of the methods, constructors or blocks.
  2. Scope:-  Local variable can be used inside methods, constructors or blocks but not outside.
  3. When Variable gets memory allocated :
    Local variable gets allocated when methods, constructors or blocks gets executed. Also when gets exits(block execution completed), local variable get destroyed.
  4. Stored Memory:- Local variables are stored in Stack memory.
  5. Default Memory:- Local variables does not have any default values, value should be provided before use.
  6. Access specifier:- Access specifiers i.e. public, protected, private cannot be used with local variables.

 Instance Variables :-

  1. Declaration:- An instance variable is declared inside the class but outside the methods,constructors or blocks.
  2. Scope:-  Instance variable can be used inside all methods,blocks and constructors within a class(not inside the static method directly)
  3. When Variable gets memory allocated :-
    Instance variable gets memory allocated when the object is created, when objects gets destroyed variable releases memory.
  4. Stored Memory:- Instance variables are stored in Heap memory.
  5. Default Memory:- Instance variables have default values like int : 0; boolean : false; object : null etc.
  6. Access specifier Access specifiers i.e. public, protected, private can be used with instance variables.
  7. How to Access:- Instance variables can be called(accessed) directly or by using object name.

 Static Variables :-

A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.

1. Declaration:

Static variables are declared with static keyword inside the class but outside the body of the methods, constructors or blocks.

2.Scope:

Static variable can be used inside all methods, constructors or blocks including static methods, constructors or blocks.

3. When Variable gets memory allocated:

When we run program and .class file is loaded in JVM, static variable gets allocated. When class file gets unloaded from JVM, static variables gets destroyed.

4.Stored memory:

Static variables are stored in non-heap memory or static memory.

5.default velues:

Static variables have default values like int : 0; boolean : false; object : null etc.

6. Access specifier:

Access specifiers i.e. public, protected, private can be used with static variables.

7. How to access:

Static variables can be called(accessed) by three ways :

1. Can be called directly.

2. By using class name i.e. Test.b (here Test is class name and b is static variable).

3. By using object reference name.

Operators In Java:-

 Definition :-

Operators are the special symbols which perform any operation on one or more operands.

For example:

int a=10, b=20; // here “=” is an assignment operator

int c=a+b; // here “=” and “+” is an assignment and arithmetic operators respectively, and “a” and “b” are operands

c++; // here “++” is an increment operator which perform operation on one operand i.e. “c”

 Types Of Operators :-

  1. Arithmetic Operator
  2. Assignment Operator
  3. Relational Operator
  4. Logical Operator
  5. Bitwise Operator
  6. Ternary Operator

 1. Arithmetic Operator :-

Arithmetic operators are used in mathematical expression in the same way that they are used in algebra.

 2. Assignment Operator :-

The assignment operator (=) is most common used operator in java. It evaluates the operand on the right hand side and then assigns the resulting value to a variable on the left hand side. The right operand can be a variable, constant, function call or expression. The type of right operand must be type compatible with the left operand.

 3. Relational Operator :-

A relational operator compares two values and determines the relationship between them.

Assume integer variable “a=10” and “b=20”, then…

Below is the list of all relational operators :-

 4. Logical Operator :-

A logical operator (sometimes called a “Boolean Operator”) is an operator that returns a boolean result that’s based on the boolean result of one or two other expressions.

Below is the list of all logical operators :-

Assume integer variable “a=true” and “b=false”, then…

 5. Bitwise Operator :-

Bitwise operators operate on individual bits of integer types (long, int, short, char, byte). If an operand is shorter than an int, it is promoted to int before doing the operations

It helps to know how integers are represented in binary. For example the decimal number 3 is represented as 11 in binary and the decimal number 5 is represented as 101 in binary.

Below is the list of all bitwise operators :-

Assume integer variable “a=60” and “b=13”, then…

 6. Ternary Operator :-

Ternary operator is used as one liner replacement for if-then-else statement. It is the only conditional operator which takes three operands and hence the named ternary.

Below is the syntax of ternary operator :-

condition ? if true : if false

Working of above operator :

If the condition evaluates to true, then the statements after the ‘?’ will execute else the statements after the ‘:’ will execute.

Below is the program to find the greater number between two numbers using ternary operator :

class Test

{

int a=10, b=20;

int result=(a>b) ? a : b;

System.out.println(“Greater of two numbers is : “+result);

}

//Output is : Greater of two numbers is : 20

Another program to find the greater number between three numbers using ternary operator :

class Test

{

int a=10, b=20, c=30;

int result=((a > b) ? (a > c) ? a : c : (b > c) ? b : c);

System.out.println(“Greater of three numbers is : “+result);

}

//Output is : Greater of three numbers is : 30

Literals In Java:-

A Literal is a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables or simply say, the value assigned to a variable is known as literal.

For example:

int a=10; // here “10” is literal

char c=’a’; // here “a” is literal

String name=”sabi”; // here “sabi” is literal

Keywords In Java:-

 Definition :-

Keywords are the predefined words having any specific meaning. These keywords cannot be used as a variable name or class name or interface name or method name or object name.

Note : true, false and null are values so they are not keywords but they also cannot be used as a variable name or class name or interface name or method name or object name.

Below is the explanation of each keyword in java (detailed explanation will be provided in further tutorials)

1. abstract :-

The abstract keyword is used to achieve abstraction in Java. “abstract” keyword is used with class and methods (not with variables). If we declare class as abstract then the method will have only declaration part, not definition part (i.e. method will not have body). If we use abstract class then the class can contain abstract method as well as concrete methods. Interfaces are also by default abstract which further contains abstract methods, but we don’t need to use abstract keyword as they are by default abstract.

2. assert :-

It is used to define an assert statement. An assert statement is used to declare an expected boolean condition in a program. If the program is running with assertions enabled, then the condition is checked at runtime. If the condition is false, the Java runtime system throws an “AssertionError”.

Syntax : assert expression1 [: expression2];

3. boolean :-

boolean is a primitive data type which can contain only true or false value.

4. break :-

break is a “Jump Statement” used to break loop or switch statement. It breaks the current flow of the program at some particular given condition.

5. byte :-

byte is a primitive data type used to declare a variable that can hold an 8-bit data values.

6. case :-

case is used with switch statements in java which is followed by the value to be compared to and a colon.

7. catch :-

catch is a keyword which is always used with try block. It is used to handle the exception object or say to provide an alternative way if an exception occurs in try block.

8. char :-

char is a primitive data type used to declare a variable that can hold unsigned 16-bit Unicode characters.

9. class :-

class is a keyword is used to declare a class followed by predefined or user defined class name.

10. continue :-

continue is a keyword used to continue the loop. It continues the loop and terminates the rest of the processing of the code within the loop for the current iteration.

11. default :-

default keyword is used to declare default values in a Java annotation. From Java 8 onwards, the default keyword can be used to allow an interface to provide an implementation of a method.

12. do :-

The do keyword is used together with while to create a do-while loop (do-while is an iterating or looping statement). In do block, the code will execute at least one time, then it will go in while to check the condition, if condition is true, it again goes in do block else goes outside do-while block.

13. double :-

double is a primitive data type used to declare a variable that can hold a 64-bit floating-point numbers.

14. else :-

else keyword is used with if block to create if-else selection or conditional statements. else block will execute if the given if condition is false.

15. enum :-

enum is a special data type that enables for a variable to be a set of predefined constants in java. According to Java naming conventions, it is recommended that we name constant with all capital letters.

First line inside enum should be list of constants and then other things like methods, variables and constructor(enum constructors are always private or default).

16. extends :-

extends keyword is used to achieve inheritance in java. If we write class B

extends A, it means A class properties are inherited in class B.

17. final :-

final keyword can be used with variable, class or method.

If we create final variable, the value becomes constant.

If we create final class, then the class cannot inherit.

If we create final method, then the method cannot override.

18. finally :-

finally keyword can be used with either try-catch block or just try block. finally block is always executed whether there is exception or not.

19. float :-

float is a primitive data type used to declare a variable that can hold a 32-bit floating-point number.

20. for :-

for keyword is used to create for looping or iterating statement. It is used to execute the statements repeatedly till the provided condition is true. If the number of iteration is fixed, it is recommended to use for loop.

21. if :-

if keyword is used to create selection or conditional statement. The if block is executed when the condition given in if block is true.

22. implements :-

implements keyword is used to achieve an inheritance in case of interface.

23. import :-

import keyword makes classes and interfaces available and accessible to the current java file or class.

24. instanceof :-

instanceof keyword is used to check whether the object is an instance of the specified class or implements an interface. If yes it returns true else false.

25. int :-

int is a primitive data type used to declare a variable that can hold a 32-bit signed integer.

26. interface :-

interface keyword is used to declare an interface. Interfaces are similar to abstract class but having all the methods of abstract type.

27. long :-

long is a primitive data type used to declare a variable that can hold a 64-bit integer.

28. native :-

native keyword is used with method to indicates that the method is implemented in native code using JNI (Java Native Interface).

29. new :-

new keyword is used to create an instance of the class by allocating memory for a new object and returning a reference to that memory.

30. package :-

package keyword is used to declare a Java package which contain similar types of classes or interfaces or packages.

31. private :-

private keyword is an access modifier which is used to indicate that the method or variable can only be accessed within the class.

32. protected :-

protected keyword is an access modifier which is used to indicate the method or variable can be accessed within package and outside the package (outside the package only when inheritance concept used).

33. public :-

public keyword is an access modifier which is used to indicate that the method or variable is accessible anywhere i.e. within same package or different package. It has the highest accessibility.

34. return :-

return keyword is used to return any value(value may be string, integer, class ref, object ref or anything) from a method when its execution is complete.

35. short :-

short is a primitive data type used to declare a variable that can hold a 16-bit integer.

36. static :-

static keyword can be used with variable, method, inner class or it can be block. The main use of this keyword is memory management. (this topic is deeply explained in OOP’s static keyword topic)

37. strictfp :-

strictfp keyword is used for restricting floating-point calculations and ensuring same result on every platform while performing operations in the floating-point variable.

38. super:-

super keyword is used to refer the parent class instance variable, method or constructor. Remember that super should be the first statement in the constructor.

39. switch :-

switch keyword creates conditional or selection statement that executes code based on test cases. If any case matches it executes the statements else it will execute the default statement.

40. synchronized :-

synchronized keyword can be used with method or block. It allows only one thread to use the resource at one time, means multiple threads cannot share the same resource at same time.

41. this :-

this keyword is used to refer current class instance variable, method or constructor. It can also used to passed as an argument in the method or constructor call.

42. throw :-

throw keyword is used in exception handling. It is used to create an exception object explicitly. throw keyword is mainly used for custom exceptions. It is followed by new instance.

43. throws :-

throws keyword is used in exception handling. It is used to declare the exception and thus indicate the caller method that particular block throws checked exception and thus we have to handle it.

44. transient :-

transient keyword is used in serialization. If you define any class attributes or variables as transient, it will not be serialized.

45. try :-

try keyword is used in exception handling which contains risky code. The try block must be followed by either catch or finally block.

46. void :-

void is the return type which indicates that the method does not return anything. Remember, void means nothing, neither 0 nor null.

47. volatile :-

Volatile keyword is used to modify the value of a variable by different threads. It is also used to make classes thread safe. It means that multiple threads can use a method and instance of the classes at the same time without any problem.

48. while :-

while keyword is looping or iterating statement. It is always recommended to use while loop if the number of iteration is not fixed.

Identifiers In Java:-

 Definition :-

Identifier is any name, it can be variable name or class name or interface name or method name or object name or package name.

 For example :-

class Test // here “Test” is an identifier(class name)

{

int a=10; // here “a” is an identifier (variable name)

void add() // here “add” is an identifier (method name)

{

int b=30, c; // here “b” and “c” are identifiers (local variables)

c=a+b;

System.out.println(c);

}

}

Rules for Identifiers :-

1.Spaces cannot be used.

int roll no=101; // “wrong”, as we cannot use space.

int roll_no=101; // “correct” as we can use spaces.

2. Only underscore(_) and dollar($) sign can be used in identifier name.

int roll_no=101; // “correct” as we can use spaces.

3. Integer value cannot be used at first position but can be used after first character.

int 1roll_no=101; // “wrong” as we can use integer at first position.

int r1oll_no=101; // “correct” as we can use integer after first character.

int roll_no1=101; // “correct” as we can use integer after first character.

4. Reserved words cannot be used as an identifier name.

int public=101; // “wrong” as public is reserved word and cannot be used as an identifier name.

int public1=101; // “correct” because we have used integer and now its not reserved word, but its not recommended to use.

Leave a Comment