Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

Java

 Java is an object oriented programming language developed by Sun micro system of USA in 1991. It is originally Called OAK by James Gosling. 

    
*java → Purely Object oriented programming 

Working of java program : Java is compiled into the byte code and then it is interpreted to machine code . 
  
Source code ⇨ byte code ⇨ Machine code 

Java installation : There  are flowing steps are used for java installation.



  •  Go to google chrome and type "install jdk" =And install java jdk file 
  •   Again go to google chrome and type " Install Intellij idea " =And install idea
* JDk : JDK stands for java development kit. It is collection of tools.it is used for developing and running the java program.

* JRE : JRE stands for java runtime environment. it helps the executing program developed in java.


                Variable and Datatype

Variable: A variable is a container that stores a value. this value can be changed during the execution of program.

example:
     int         number         = 8 
     ↙             ↘               ↘
  Data type     Variable name      Value 

* Rules for declaring a variable name: There are flowing rules for declaring a variable name.

     
   1. Must not begin with a digit.

   2. Name is case sensitive.

   
   3. Should not be keyword.

   4. White space are not allowed.

  
   5.It contain alphabet,doller($),character and digit. if the other condition are invalid

* Datatype: There are two type of datatype. Which are given below


      1. Primitive Datatype 

   2. Non Primitive Datatype


             Primitive    Data   type
           ⬋        ⬊     ⬊        ⬊
      Integral    Float    Char     Boolean                                      
  byte int long  float double (' ')    (t/f)

1.Primitive Datatype: Java is a statically datatype: The variable must be declared byte code are used. There are following eight (8) primitive datatype.

1. Byte : value range from ➨ -128 to 127

          take one(1) byte ➨ 
          Default value is Zero 

2.Short : value range from ➨ -65536 to 65535
          take two(2) byte  ➨
          Default value is Zero 

3. Int  : value range from  ➨ 

          take four(4) byte ➨
          Default value is Zero

4.Float : value range from  ➨ (sec docs)

          take four(4) byte ➨ 
          Default value is Zero 

5. Long : value range from  ➨ (sec docs)
         take eight(8)byte  ➨ 
          Default value is Zero 

6.Double: value range from  ➨ (sec docs)

          take eight(8)byte ➨ 
          Default value is Zero 

7.Char :  value range from ➨ 0 to 65535 

          take two(2) byte ➨ 
          Default value is Zero 

8.Boolean: value can be true ya false

           size depend upon jvm
           Default value is false

* Literal : A constant value that can be assigned to the variable name is called as a literal. 

  example:
      101     ➨ Integer   literal 
      101f    ➨ Float     literal 
      101.1   ➨ Double    literal
      'A'     ➨ Character literal 
      True    ➨ Boolean   literal
     "Pankaj" ➨ String    literal

* Keyword: Words are reserved and used by the java compiler. They cannot be used as a variable name, identifier. There are Fourty eight keyword avilable in java compiler.


java keyword list | Reading, Interface, Java



* Reading data from keyword: In order to read data from the keyword in java has Scanner class.
       Scanner class has a bit of methods to read the data from the keyword.
   
   Scanner sc = new Scanner(System.in)  
                               ⬊
                        Read from the keyword
 Example:
      int   a   =  S .nextInt();
                                                                                                                                                                                      ⬊
                  Method to read from keyword

* Operator and Expression: Operator are used to perform operation on variable and value.

There are following types of operator.

Example:

          4       +        8      =           12
                        ⬇                ⬇                   ⬇                                 ⬇
                     operand  operator    operand             result                 
Types Of Operator

  • Arithmetic Operators => [+, -, *, /, %, ++, --]
  • Assignment Operators => [=, +=]
  • Comparison Operators => [==, >=, <=]
  • Logical Operators => [&&, ||, !]
  • Bitwise Operators => [&, |] (operates bitwise)

Arithmetic operators cannot work with Booleans.

% operator can work on floats and doubles

Precedence of operators


The operators are applied and evaluated based on precedence. For example (+, -) has less precedence compared to (*, /). Hence * and / are evaluated first.

In case we like to change this order, we use parenthesis ().

Associativity


Associativity tells the direction of the execution of operators. It can either be left to right or vice versa.

/ * -> L to R

+ - -> L to R

++, = -> R to L

Resulting data type after arithmetic operation


Result = byte + short -> integer

Result = short + integer -> integer

Result = long + float -> float

Result = integer + float -> float

Result = character + integer -> integer

Result = character + short -> integer

Result = long + double -> double

Result = float + double -> double

Increment and Decrement operators


a++, ++a (Increment Operators)

a--, --a (Decrement Operators)

 Introduction to Strings

  • A string is a sequence of characters.

  • A string is instantiated as follows:

  • String name;

  • name = new String(“Pankaj”);

  • String is a class but can be used as a data type.

  • Strings are immutable and cannot be changed.

  • String name = “Pankaj”

  • (Here, name is a reference, and “Pankaj” is an object.)


Different ways to print in Java

We can use the following ways to print in Java:

  • out.print() // No newline at the end
  • out.println() // Prints a new line at the end
  • out.printf()
  • out.format()

System.out.printf(“%c”,ch)

{ %d for int

  %f for float

  %c for char

  %s for string }

 String Methods in Java

String Methods operate on Java Strings. They can be used to find the length of the string, convert to lowercase, etc.

Some of the commonly used String methods are:

String name = “Harry”;

(Indexes of the above string are as follows: 0-H, 1-a, 2-r, 3-r, 4-y)

  1. length() – Returns the length of String name. (5 in this case)
  2. toLowerCase() – Returns a new String which has all the lowercase characters from the String name.
  3. toUpperCase() – Returns a new String which has all the uppercase characters from the String name.
  4. trim() – Returns a new String after removing all the leading and trailing spaces from the original string.
  5. substring(int start) – Returns a substring from start to the end. Substring(3) returns “ry”. [Note that index starts from 0]
  6. substring(int start, int end) – Returns a substring from the start index to the end index. The start index is included and the end is excluded.
  7. replace(‘r’, ‘p’) – Returns a new string after replacing r with p. Happy is returned in this case. (This method takes char as argument)
  8. startsWith(“Ha”) – Returns true if name starts with string “Ha”. (True in this case)
  9. endsWith(“ry”) – Returns true if name ends with string “ry”. (True in this case)
  • charAt(2) – Returns the character at a given index position. (r in this case)
  • indexOf(“s”) – Returns the index of the given string.

For e.g. name.indexOf(“ar”) returns 1 which is the first occurrence of ar in string “Harry”, -1 otherwise.

  • indexOf(“s”, 3) – Returns the index of the given String starting from index 3(int). (-1 is returned in this case)
  • lastIndexOf(“r”) – Returns the last index of the given string. (3 in this case)
  • lastIndexOf(“r”,2) – Returns the last index of the given string before index 2.
  • equals(“Harry”) – Returns true if the given string is equal to “Harry” false otherwise [Case sensitive]
  • equalsIgnoreCase(“harry”) – Returns true if two strings are equal ignoring the case of characters.

Escape Sequence Characters


The sequence of characters after backslash ‘\’

= Escape Sequence Characters

Escape Sequence Characters consist of more than one character but represent one character when used within the strings.

Examples: \n (newline), \t (tab), \’ (single quote), \\ (backslash), etc.

All these are decisions which depends on a certain condition being met. In java, we can execute instructions on a conditional being met.

Decision-making instructions in Java

  • If-Else Statement
  • Switch Statement

If-Else Statement

The syntax of an if-else statement in C looks like that of C++ and JavaScript. Java has a similar syntax too. It looks like.

Syntex

 if (condition-to-be-checked) {

statements-if-condition-true;

}

else {

statements-if-condition-false;

}

Example If-else Statement 

int a = 29;

if (a>18) {

System.out.println(“You can drive”);

}

Relational Operators in Java

Relational operators are used to evaluate conditions (true or false) inside the if statements.

Some examples of relational operators are:

== (equals)

>= (greater than or equals to)

> (greater than)

< (less than)

<= (less than or equals to)

!= (not equals)

Note: ‘=’ is used for assignment whereas ‘==’ is used for equality check.

The condition can be either true or false.

Logical Operators

&&, || and ! are the most commonly used logical operators in Java.

These are read as:

&& - AND

|| - OR

! – NOT

The above logical operators are used to provide logic to our Java programs.

AND Operator

Evaluates to true if both the conditions are true.

Y && Y = Y                # Y – True and N - False

Y && N = N

N && Y = N

N && N = N

OR Operator

Evaluates to true when at least one of the conditions is true.

Y || Y = Y                   # Y – True and N - False

Y || N = Y

N || Y = Y

N || N = N

NOT Operator

Negates the given logic (true becomes false and vice-versa)

!Y = N                         # Y – True and N - False

!N = Y

Else-if clause

Instead of using multiple if statements, we can also use else if along with if thus forming an if-else-if-else ladder.

Using such kind of logic reduces indents. Last else is executed only if all the conditions fail.

                     Syntex

 if (condition1) {

            //Statements;

else if {

            // Statements;

}

else {

            //Statements

}

        Switch Case-Control Instruction

Switch-Case is used when we have to make a choice between the number of alternatives for a given variable.

                  Syntex

 Switch(var) {

Case C1:

//Code;

break;

Case C2:

//Code;

break;

Case C3:

//Code

break;

default:

//Code

Var can be an integer, character, or string in Java.

A switch can occur within another but in practice, this is rarely done.


Post a Comment

0 Comments