Sunday, 12 August 2018

Java Concepts -2:

Variable Declaration:
---------------------

Variable is a basket of memory where we can store data
Variables are used to store information to be referenced and used by programs

There are Eight Types of Primative DataTypes in Java for Numeric Values

Syntax:  Datatype VariableName=Data

The Following are Eight Types of DataTypes:

1. Byte (Memory 8 bits)
Range: -128 to 127 (2^7)

Exp:
byte myByte = 127;
System.out.println(myByte);
//Console Output Shown: 127

2. Short (Memory 16 bits)
Range: -32768 to 32767 (2^15)

Exp:
short myShort = 32750;
System.out.println(myShort);
//Console Output Shown: 32750


3. Int (Memory 32 bits)
Range: upto 18446744073709551616 (2^64)

Exp:
int myInt = 1000000000
System.out.println(myInt);
//Console Output Shown: 1000000000


4. Long (Memory 64 bits)
Range: Long Values

Exp:
long myLongNumber = 1000000000000000000l //(we have to add L at the end as suffix)
System.out.println(myLongNumber);
//Console Output Shown: 1000000000000000000


5. Float (32 bit) (Used for Decimal values)
Exp: 
float myFloat= 23.45f; //(we have to add F as suffix)
System.out.println(myFloat);
//Console Output Shown: 23.45


6. Double (64 bit) (Used for Decimal values)
Exp: 
double myDouble= 2334.4587;
System.out.println(myDouble);
//Console Output Shown: 2334.4587

7. Char (16 bit)
Exp: 
char myGender='M' //(Single quotes only for single character)
System.out.println(myGender);
//Console Output Shown: M


8. Boolean for true/false
Exp:
boolean myStatus= true;
System.out.println(myStatus);
//Console Output Shown: true

NOTE:  String Class in Java is used for sequence of characters
Exp:
String myVal="SeleniumClass"; //(4* 16 bits)
System.out.println(myVal);
//Console Output Shown: SeleniumClass

-------------------------------------------------------------------------------------
Some of the Important string methods:
------------------------------------
1. length():
To find number of characters in a given string
Exp:
String str1="SeleniumClass";
System.out.println(str1.length());
//Console Output Shown: 13


2. charAt():
To read character from main string based on index
Exp:
String str2="SeleniumClass"; //to read "m"
System.out.println(str2.charAt(7));
//Console Output Shown: m


3. indexOf():
To find index of given character in main string
Exp:
String str3="SeleniumClass"; //to find index of "C"
System.out.println(str3.indexOf("C"));
//Console Output Shown: 8
/*indexing starts from 0,
in the above example S is 0, e is 1, l is 2.....C is 8*/


4. toUpperCase():
It will convert given string into upper case
Exp:
String str4="SeleniumClass";
System.out.println(str4.toUpperCase());
//Console Output Shown: SELENIUMCLASS


5. toLowerCase():
It will convert given string into lower case
Exp:
String str5="SeleniumClass";
System.out.println(str5.toLowerCase());
//Console Output Shown: seleniumclass


6. equals():
To compare the strings
Exp:
String str6="SeleniumClass";
System.out.println(str6.equals("Seleniumclass"));
//Console Output Shown: False

String str7="SeleniumClass";
System.out.println(str7.equals("SeleniumClass"));
//Console Output Shown: True


7. equalsIgnoreCase():
To compare the strings, It is caseinsensitive
Exp:
String str8="SeleniumClass";
System.out.println(str8.equalsIgnoreCase("SeleNiumClass"));
//Console Output Shown: True

String str9="SeleniumClass";
System.out.println(str9.equalsIgnoreCase("Seleniumclass"));
//Console Output Shown: True


8. startsWith():
To compare prefix value of a string(i.e starting characters)
Exp:
String str10="SeleniumClass";
System.out.println(str10.startsWith("Se"));
//Console Output Shown: True

String str11="SeleniumClass";
System.out.println(str11.startsWith("Ae"));
//Console Output Shown: False


9. endsWith():
To check suffix value of a string (i.e. end characters)
Exp:
String str12="SeleniumClass";
System.out.println(str12.endsWith("Class"));
//Console Output Shown: True

String str13="SeleniumClass";
System.out.println(str13.endsWith("Cless"));
//Console Output Shown: False


10. contains():
To check substring availability in a main string
Exp:
String str14="SeleniumClass";
System.out.println(str14.contains("ium"));
//Console Output Shown: True

String str15="SeleniumClass";
System.out.println(str15.contains("mum"));
//Console Output Shown: False


11. concat():
To concat the strings
Exp:
String str16="Selenium";
String str17="Class";
System.out.println(str16.concat(str17));
//Console Output Shown: SeleniumClass

String str18="Selenium";
String str19="Class";
System.out.println(str18+str19);
//Console Output Shown: SeleniumClass


12. trim():
It will remove leading and trailing spaces for a given string

Exp:
String str20=" SeleniumClass ";
System.out.println(str20.length());
//Console Output Shown: 15

String str21=" SeleniumClass ";
System.out.println(str21.trim().length());
//Console Output Shown: 13


13. replace():
To replace substring in a main string
Exp:
String str22="SeleniumClass";
System.out.println(str22.replace("s", "@"));
//Console Output Shown: SeleniumCla@@
//replace is casesensitive so capital S is not replaced

String str23=" Sele nium Cla ss ";
System.out.println(str23.trim());
//Console Output Shown: Sele nium Cla ss
//trim only trims at the beginning and ending spaces

String str24=" Sele nium Cla ss ";
System.out.println(str24.replace(" ", ""));
//Console Output Shown: SeleniumClass

14. subString():
To read substring from mainstring
ex:
String str25="SeleniumClass";
System.out.println(str25.substring(5));
//Console Output Shown: iumClass

String str26="SeleniumClass";
System.out.println(str26.substring(5, 7));
//Console Output Shown: // iu

String str27="SeleniumClass";
System.out.println(str27.substring(5, 8));
//Console Output Shown:// ium

15. isEmpty():
To check value in a variable is Empty or not









No comments:

Post a Comment