Posts

Featured post

Constructors in java

 Java Constructors Constructor is nothing but a method with a class name It doesn't return any values  We don't write any return type of the constructor It is used to initialise the values when the object is created In java there is constructor called default constructor it is automatically initialize when there is no constructor is assigned We can also give parameters in the constructor so at the time of initialize we can assign some value into the class. It is known as parametric construction If we create a any constructor in a class then the default constructor will not work so we need to write the default constructor also if we want to use them Ex: 1) Default constructor class Hello{ public int a=10; public int b=20; } class Hel{ public static void main (String []args){ Hello h=new Hello();// using default constructor  System.out.print(h.a+h.b);//1020 } 2) Constructor with no parameters   class Hello{ public Hello(){ System.out.print("Hello"); } } class Hel{ publi

Java String Methods

  String Methods in java String.toUpperCase()   returns the String into UpperCase String.toLowercase()   returns the String into LowerCase String.charAt(indexValue)   returns the character at given index number String.replace(oldString/char,newString/char)   returns the String which is replaced with new character/String by old String/character  String.replaceFirst(oldString,newString)   return the String   which is replace of first occurance of old character/String into new character/String String.contains(checkString/character)   returns boolean value if the contain the given char/String String.concat(newString)   returns the concatenation of new String at the end of oldString, we can also use + to concat the Strings String.equals(checkString)    returns the boolean value by checking the values of 2 strings are exactly matched (case sensitive) String.equalsIgnoreCase(checkString)   returns the boolean value by checking the values of 2 strings are exactly matched even they are

Java Strings Basics

  Strings in java Java strings are immutable (it cannot be changes within it self) The group of characters is known as string It string represent with the text inside the double quotes The index number start from 0 (zero) upto length-1 All the strings stores in string constant pool which returns the same object for the strings until the we  assign a new instance for the string 1)Assigning a String to the variable: In java we can assign a string in different ways Ex : String name=""; n ame="word"; String name="word"; char ch={'w','o','r','d'}; S tring name=new String(ch); String name=new String("name"); Ex;-  class StringAssignment{ public static void main ( String []args){ String word1 ="Daniel"; String word2="Daniel"; //Here both references with return same literals // == used to check the literals and .equals is used to chech the values String word3=new String ("Daniel"); System.out.

Class VS Object

  Classes in java Classes are used to store our code  Classes in java can be created using class keyword Class is like a blueprint where we can write all of our requirements   Ex:-  fileName- Demo.java class Hello{  public void print(){ System.out.print("It is in print() in Hello class"); } } Objects in java Objects are used to store the attributes of a class It is like instance of a class It stores all attributes of a class If we want to access the features of a class we use objects Ex:- fileName- Demo.java       <Same file> class Demo { public static void main (String []args){ Hello h=new Hello(); // 'Hello h 'is a reference of class Hello class // new Hello() is a instance of a class Hello // By combining the two we get object it means it stores the all attributes/ features of class Hello  // .(dot) is used to access the specific features of the class  h.print(); } }

Looping Statements

 Looping Statements or Iterative Statements These are used to run the set of statements repeatedly. Here the statements run repeatedly until the given condition is false. We need to write a proper condition so that the loop runs finite number of times. Types of loops: while do-while for foreach 1)while loop: Here the loop executes until the given condition is false. The Initialization / declaration can be done at anywhere before the loop. Syntax: while(condition){ //statements which are needed to be execute Increment/Decrement; } 2)do-while loop: Here the statements are executes atleast one time , even the given condition is false. It is exit control loop whereas for,while are entry control loops. Syntax: do{ //statements which are needed to be execute }while(conditon); 3)for loop: Here the number of Iterations can be known. Here the initialization , Declaration , Condition , Increment / Decrement are present a single place. We can place multiple Declarations , Increments / Decrements

Flow Control Statements

 Flow Control Statements These statements are to control the flow of the program.  By using these statements, we can get the required output easily. Types of flow control statements: Conditional Statements Looping Statements Jump Statements Conditional Statements: Used to control the flow of the program by using conditions if if-else nested if if-else-if ladder switch Looping Statements: Used to run the set of code / a block repeatedly until the condition is false while do-while for for each Jump Statements: Used to skip or break the iteration or to exit the block continue break

Conditonal Statements

Conditional Statements By using the Conditional Statements we can easily control the flow of the program. if statement                 if - else statement                Nested if statement  if else if ladder switch statement 1) if statement: It executes the set of code / block when the given condition is true. It doesn't execute the block when the given condition is false. Syntax: if(condition){ //statements to be execute when the given condition is true } 2) if - else statement: It is used to execute the different block of code alternatively. It is similar to if statement. If the condition is matched/true then if block statements will executes. If the condition is not matched/false then else block statements will executes. else is a extension of if statement. Without if we cannot use else statement. Syntax: if(condition){ //statements when condition is true }else{ //statements when condition is false } 3) Nested if statement: It is used to check the 2 or more if conditions. Here

Operators in java

Operators Operators Arithemetic Operators + Addition 1+2 - Subtraction 2-3 * Multiplication 3*4 / Division 4/3 % Modules 5%3 Unary Operators ++ Increment a++ or ++a -- Decrement a-- or --a + Unary Plus +a - Unary Minus -a Assignment Operators = equals a=a+b a+=b a=a-b a-=b a=a*b a*=b a=a/b a/=b a=a%b a%=b Relational Operators < Lessthan a < b > Greaterthan a > b <= Lessthan or Equal to a <= b >= Greaterthan or Equal to a >= b == Equalto a == b != Not Equal to a != b Bitwise Operators & Bitwise AND a&b | Bitwise OR a|b ^ Bitwise NOT a^b Logical Operators && Logical AND a&&b || Logical OR a||b ! Logical NOT !TRUE or !FALSE

Scope of Variable

 Scope of Variables The Accessibility of the variables in a program. These are classified into 3 types: Instance variables Class variables Local variables Instance Variables These variables are available until there is a memory allocated to the instance of the class. Example: class MyClass{ public MyClass(){  message="Welcome"; } String message; } public class Main{ public static void main(String gdv[]){ MyClass d =new MyClass(); System.out.print(d.message+" "+new MyClass().message); } } OUTPUT: Welcome Welcome Class Variables  These variables are available until the end of the entire program execution. These can be accessed anywhere throughout the program. Example: class MyClass{ static String message="Hello"; public static void main(String []gdv){ System.out.print(message); } } OUTPUT: Hello Local Variables These variables are only available until the method is executed. After the method finishes executes then these variables are unavailable. These varia

Variables

VARIABLES Variables are used to store the values of a specific data type. Variable name can be started with Letters or $ and _ symbols. Variable name Start with Lowercase Alphabets. Variable name can be succeeded with numbers. Variable name cannot be started with numbers. No other Symbols are used in variable name except $ and _ . Syntax: <datatype> <variablename> ; Correct way to declare a Variable  int height; String name; float num1; int _number; boolean $id; Incorrect way to declare a Variable height int; int %num; String 1name;

DATA TYPES

Data Types are used to specify the type of data that is used to store. int                  used to store integer value. char                used to store characters and we use single quotations to specify character . float                used to store the decimal values up to 6 decimals. double            used to store the decimal values up to 8 decimals. byte                used to store the byte code values. short              used to store the short Integer values. long                used to store the long Integer values. Examples :- int  106 0 -635 char 'q' 'L' '+' '$' float 61.562f -96.386245f 5462.15f double 0.3215 45.21 78.96325874 byte 127 -128 65 short 32767 -32768 long  45255625L -54255542L DataTypes