Java Chapter 2 – Common futures of programming language

Home  >>  Uncategorized  >>  Java Chapter 2 – Common futures of programming language

Java Chapter 2 – Common futures of programming language

On August 24, 2019, Posted by , In Uncategorized, With No Comments

Let’s try to understand basics of any programing language before moving to Java:

Below are the common / basic futures of any programming language:

  1. Data Types
  2. Variables
  3. Operators
  4. Conditional Statements
  5. Control Statements (loops)
  6. Arrays
  7. Function / Methods

Data Types:

Data types are used to allocate memory and specify what type of data you are using in the program.

All the data types are keywords which are having built in meaning. Below are the few data types in Java.

  1. int: which is used to store integer value and it will not accept any decimal value. It allocates 4 bytes of memory.
  2. long: which is used to store integer value and it will not accept any decimal value. It allocates 8 bytes of memory
  3. float: which is used to store decimal value. It allocates 4 bytes of memory
  4. double: which is used to store decimal value. It allocates 8 bytes of memory
  5. char: which is used to store single character and it use to take only one byte.
  6. Short: which use to take 2 bytes of memory and stores integer value.
  7. Boolean: which takes only true or false
  8. Byte: which takes only byte memory

 

String: is not a data type but it is predefined class. It allows to store set of characters.

Variables:Variables are nothing but in which data is stored. It is nothing but memory location name by which you can refer your data.

Declarition and initilization of variable

datatypevariablename=value

Ex:int a=10;

int b=20

int c=a+b

Rules to follow for declaring the variable name:

  1. Should start with lower case and first letter of the secondword should start with captial letter. i.e. firstName, orderNumber
  2. Can start with _ (under score)
  3. Can start with $
  4. First letter cannot be a capital letter
  5. It can contain numbers but not in the middle
  6. No other special symbols allowed

Example for data types and using variables:

public class JavaDataTypes {

 

public static void main(String[] args) {

inta,b,c;

a=10;

b=20;

c=a+b;

System.out.println(“Addition of a and b is ” +c);

 

}

}

Operators:

  1. Arithmetic:
    • +
    • *
    • /
    • % (moduls) which gives remainder.
  2. Relation Operator:
    • >
    • <
    • >=
    • <=
    • !=

3. Equality Operator / comparison:

    • == which checks for equality

4. Concatenation Operator:

    • +: is used to value with string or string with value
      Ex: String a=”Ashok”

      int b=10

      a+b=Ashok10

      b+a=10Ashok

Logical Operators

  • &&(And operator)
  • ||(OR operator)
  • ! (logical not operator)

Conditional statements: are used to execute set of statements based on the condition satisfaction.

  1. if
  2. else
  3. else if
  4. nested if

 

if: Here we write one condition and execute set of statements based on condition. Statements under if will be executed only if condition is true.

Syn: if(condition)

{

statements;

}

This block of statements will be executed only if condition is true.

if-else: It’s same as if but we will one additional else. In if we understand that it will execute only if condition is true but we do not have any else statement. Here if condition is false then it will go and execute else statements. For else we don’t need to give any condition and it will execute only if condition is false

syn: if(condition)

{

statements;

}

else

{

statements;

}

 

else-if: Using if and else we are able to check for two conditions only. Let’s take an example you want to write a program which should display whether student is pass or fail based on marks given. So here using if else we can display just pass or fail. But what if I want to write a program where it should display student division based on marks provided like First division, second division and just pass. So here we are checking for multiple conditions one for first class, another one for second class and for just pass. We can achieve this using else-if.

Syntax: if(condition)

{

statements;

}

else if(condition)

{

statements;

}

else

{

statements;

}

 

Nested if: writing if inside if

 

Switch: Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int.

Syn:

Switch(expression)

{

case“a”:

statements;

break;

case“b”:

statements;

break;

default:

statements;

 

}

Leave a Reply

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