This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• Java defines several Escape sequences to represent Special characters.
• An escape sequence begins with a backslash character (\) and indicates a character or characters that follow should be interpreted in some special way.
What does \t doWhat does \t do• \t - tab• When u use this in a println
statement • System.out.println(“Roses are red,\t Violets are blue,\t,Sugar is sweet”)
• OUTPUT
Roses are red, Violets are blue , Sugar is sweet
Escape Seq Meaning \b backspace \t tab \n newline \r carriage return \” Double quote \’ Single quote \\ backslash
What does \n doWhat does \n do
• \n - newline• When u use this in a println statement
• System.out.println(“Roses are red,\n Violets are blue,\n Sugar is sweet”);
OUTPUT
Roses are red, Violets are blue , Sugar is sweet
What does \” doWhat does \” do
• \n” - Double quote• When u use this in a println statement
• System.out.println(“\ Roses are red,\”Violets are blue”,\n Sugar is sweet”);
OUTPUT
Roses are red, “Violets are blue” , Sugar is sweet
What does \’ doWhat does \’ do
• \’ - Single quote• When u use this in a println statement
• System.out.println(“Roses are red, \‘Violets are blue\’Sugar is sweet.”);
OUTPUT
Roses are red, ‘Violets are blue’, Sugar is sweet.
What does \\ doWhat does \\ do
• \\ - backslash
• When u use this in a println statement
• System.out.println(“Roses are red\\Violets are blue\\Sugar is sweet.”);
OUTPUT
Roses are red\Violets are blue\Sugar is sweet.
What does \b doWhat does \b do
• \’ - backspace
• When u use this in a println statement
• System.out.println(“Roses are red, \bViolets are blue,\bSugar is sweet.”);
OUTPUT
Roses are red, Violets are blue,Sugar is sweet.
String ConcatenationString Concatenation• Strings that are printed using a print statement
can be concatenated using +
• Eg• System.out.println(“Roses are red”+“ ” +“Violets are blue” );
OUTPUT
Roses are red Violets are blue
VARIABLES /IDENTIFIERSVARIABLES /IDENTIFIERS• A Variable is a name for a location in
the memory used to hold a data value
num=25
25
num
memory
• Eg public class keys { Public static void main(String args[]) { int key ; //declaration statement key = 88; //initialization statement System.out.println(“A piano has” + key + “keys.”); System.out.println( key );} OUTPUT:
A piano has 88 keys 88
Primitive Data typePrimitive Data typeThere are 8 data types in java• Integer - has 4 subsets (byte, short, int,long)• Floating point numbers - 2 subsets ( float, double)• Boolean• Character
• Examples: • int num; // num can store whole numbers
• float average; // average can store decimal numbers
• char ch; // stores alphabets
• Boolean value; // stores either True/False
INTIALISATION STATEMENTINTIALISATION STATEMENT
• num=23;• average=23.00;• ch= ‘s’;• value=true;
Assignment statementAssignment statement• Assignment statement –assigns a value to a variable.Public class assign{Public static void main (String args[]){ int no; no=89; int num=56; //assignment st System.out.println(num); System.out.println (“The no is :“+no); }
Arithmetic expressionArithmetic expression• An expression is a combination of one or more
operators and operands• the basic arithmetic operations defined for integer type
and floating point type are• addition(+),subtraction(-,multiplication(*),division(/).• Java has one more arithmetic operation remainder(%)• 17%4 will return 1• a= b + c; a,b,c – operands + -- operator
expression should be syntactically correct no of left parenthesis should be =no of right parenthesis Result=((19+6)%3)*3); //invalid Result=2+8-6; usually u start from left when same level of precedence
objectsobjects• Class is used to define an object• Class name is nothing but type of object;• String name; //declaration 1st step• The above declaration creates a reference to
string object• No object actually exist• To create an obj use NEW operator:• name=new String (“Adam”); //obj created 2nd
step
• 1st and 2nd can be combined• String name=new String (“Adam”);• The String class has a no of methodsWhich its objects can useThey are