The Java compiler translates these tokens into Java bytecode. Further, these bytecodes are executed inside the interpreted Java environment. Types of Tokens Java token includes the following: o Keywords o Identifiers o Literals o Operators o Separators o Comments Keywords: These are the pre-defined reserved words of any programming language. Each keyword has a special meaning. It is always written in lower case. Java provides the following keywords: w 01. abstract 02. boolean 03. byte 04. break 05. class 06. case 07. catch 08. char 09. continue 10. default 11. do 12. double 13. else 14. extends 15. final 16. finally 17. float 18. for 19. if 20. implements 21. import 22. instanceof 23. int 24. interface 25. long 26. native 27. new 28. package 29. private 30. protected 31. public 32. return 33. short 34. static 35. super 36. switch 37. synchronized 38. this 39. thro 40. throws 41. transient 42. try 43. void 44. volatile 45. while 46. assert 47. const 48. enum 49. goto 50. strictfp Identifier: Identifiers are used to name a variable, constant, function, class, and array. It usually defined by the user. It uses letters, underscores, or a dollar sign as the first character. The label is also known as a special kind of identifier that is used in the goto statement. Remember that the identifier name must be different from the reserved keywords. There are some rules to declare identifiers are: o The first letter of an identifier must be a letter, underscore or a dollar sign. It cannot start with digits but may contain digits. o The whitespace cannot be included in the identifier. o Identifiers are case sensitive. Some valid identifiers are: 1. PhoneNumber 2. PRICE 3. radius 4. a 5. a1 6. _phonenumber 7. $circumference 8. jagged_array 9. 12radius //invalid Literals: In programming literal is a notation that represents a fixed value (constant) in the source code. It can be categorized as an integer literal, string literal, Boolean literal, etc. It is defined by the programmer. Once it has been defined cannot be changed. Java provides five types of literals are as follows: o Integer o Floating Point o Character o String o Boolean Literal Type 23 int 9.86 double false, true boolean 'K', '7', '-' char "javatpoint" String null any reference type Operators: In programming, operators are the special symbol that tells the compiler to perform a special operation. Java provides different types of operators that can be classified according to the functionality they provide. There are eight types of operators in Java, are as follows: o Arithmetic Operators o Assignment Operators o Relational Operators o Unary Operators o Logical Operators o Ternary Operators o Bitwise Operators o Shift Operators Operator Symbols Arithmetic +,-,/,*,% Unary ++ , - - , ! Assignment = , += , -= , *= , /= , %= , ^= Relational ==, != , < , >, <= , >= Logical && , || Ternary (Condition) ? (Statement1) : (Statement2); Bitwise &,|,^,~ Shift << , >> , >>> Separators: The separators in Java is also known as punctuators. There are nine separators in Java, are as follows: 1. separator <= ; | , | . | ( | ) | { | } | [ | ] Note that the first three separators (; , and .) are tokens that separate other tokens, and the last six (3 pairs of braces) separators are also known as delimiters. For example, Math.pow(9, 3); contains nine tokens. o Square Brackets []: It is used to define array elements. A pair of square brackets represents the single-dimensional array, two pairs of square brackets represent the two-dimensional array. o Parentheses (): It is used to call the functions and parsing the parameters. o Curly Braces {}: The curly braces denote the starting and ending of a code block. o Comma (,): It is used to separate two values, statements, and parameters. o Assignment Operator (=): It is used to assign a variable and constant. o Semicolon (;): It is the symbol that can be found at end of the statements. It separates the two statements. o Period (.): It separates the package name form the sub-packages and class. It also separates a variable or method from a reference variable. Comments: Comments allow us to specify information about the program inside our Java code. Java compiler recognizes these comments as tokens but excludes it form further processing. The Java compiler treats comments as whitespaces. Java provides the following two types of comments: o Line Oriented: It begins with a pair of forwarding slashes (//). o Block-Oriented: It begins with /* and continues until it founds */. a=a-(a--)-(--a) a=10-(10- 8) a=10-2 a=8 L->R When we try to assign a variable of large-sized type to a smaller sized type, Java will generate an error, incompatible types: possible lossy conversion, while compiling the code. public class ICSE2006_6 { public static void main()throws Exception { String s="January 26 is celebrated as the Republic Day of India"; s=s.replace("January", "August"); s=s.replace("26", "15"); s=s.replace("Republic", "Independence"); System.out.println(s); } } (f) What is the output of the following : (a) System.out.println (“Append :” + 4 + 2); (b) System.out.println (“Sum:” + (4+2)); Append:42 Sum: 6 (g) Evaluate the following expressions, if the values of the variables are a = 2, b = 3 and c = 9. (i) a – (b++) *( – – c) 2 – 3*8 2-24 -22 (ii) a * (++b) % c 2*4%9 8%9 8 A package that is invoked by default. java.lang; (ii) A keyword, to use the classes defined in package. import Two types of Java Programs (i) Java applets (ii) Java applications public class test { public static void main(String args[]) { double p = Math.ceil(8.3) + Math.floor(-3.2)+Math.rint(4.5); double q = Math.abs(Math.round(Math.max( -4.4, -9.6))); System.out.println (q); System.out.println (q); } } In case of 0.5 at decimal places it will round off to nearest even double value. public class test { public static void main(String args[]) { String str1 = "great"; String str2= "Minds"; System.out.println(str1.substring(0,2).concat(str2.substring(1))); System.out.println(( "WH"+(str1.substring(2).toUpperCase() ))); } } grinds WHEAT State the two kinds of data types. Ans: The two kinds of data types in Java are primitive and reference data types. d. Define impure function. Ans: Impure Function: A function that brings about a change in the argument that it receives. Its arguments will always be reference types. It may or may not return value. In other words, an impure function brings about a change in the state of the function. This change in state is called the side effect of calling an impure function. What is wrapper class? Give example. e. A wrapper class is a class that wraps a primitive data type. Example Double, Float, Integer import java.util.*; class series { public static void main(String arg[]) { System.out.println("1.series_1 2.series_2 "); Scanner sc=new Scanner(System.in); System.out.println("enter your choice"); int choice=sc.nextInt(); switch(choice) { case 1: double sum=0.0; int x=2; for (int i=1;i<=20;i++) { if (i%2==0) sum =sum-Math.pow(x,i); else sum =sum+Math.pow(x,i); } System.out.println("sum is ="+sum); break; case 2 : int s=0; for(int i=1 ;i<=5 ;i++) { s=s*10+1 ; System.out.print(s +" " ); } break; default :System.out.println("invalid choice"); } } } class overload { public static void check(String s ,char ch) { int c=0; for(int i=0;i<s.length();i++) { if(s.charAt(i)==ch) c++; } System.out.println("number of "+ ch+ " present is="+c); } public static void check(String s1) { s1=s1.toLowerCase(); for(int i=0;i<s1.length();i++) { char ch = s1.charAt(i); if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') System.out.print(ch+" "); } } public static void main(String arg[]) { check("success", 's'); check("computer"); } } public class Selection { String m[]=new String[40]; Selection(String z[]) { m=z; } void sort() { int i, p, j; String s,t; for (i=0;i<40;i++) { s=m[i]; p=i; for (j=i+1;j<40;j++) { if (m[j].compareTo(s)>0) { s=m[j]; p=j; } } t=m[i]; m[i]=m[p]; m[p]=t; } for (i=0;i<40;i++) System.out.println(m[i]); } } /* Program to Print Diamond Shape */ import java.util.Scanner; public class Diamond { public static void main(String args[]) { int n, i, j, space = 1; System.out.print("Enter the number of rows: "); Scanner s = new Scanner(System.in); n = s.nextInt(); space = n - 1; for (j = 1; j <= n; j++) { for (i = 1; i <= space; i++) { System.out.print(" "); } space--; for (i = 1; i <= 2 * j - 1; i++) { System.out.print("*"); } System.out.println(""); } space = 1; for (j = 1; j <= n - 1; j++) { for (i = 1; i <= space; i++) { System.out.print(" "); } space++; for (i = 1; i <= 2 * (n - j) - 1; i++) { System.out.print("*"); } System.out.println(""); } } } /* Java Program to Check whether an inputted Number is Strontio or Not i.e. 1386 * 2 = 2772 */ import java.util.*; public class Strontio { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter the number: "); int num=sc.nextInt(); num=(num*2%1000)/10; if(num%10==num/10) System.out.println(n+ " is a strontio number."); else System.out.println(n+ " is not a strontio number."); } } * Program to Print following pattern */ 13579 35791 57913 79135 91357 public class test { public static void main(String args[]) { int i,j,z=1,w; for(i=1;i<=5;i++) { w=z; for(j=1;j<=5;j++) { System.out.print(w%10); w=w+2; } z=z+2; System.out.println(); } } } /* Java Program to Perform Basic String Operations */ import java.util.Scanner; import java.util.Arrays; public class StringMethods { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter String-1"); String s1=sc.nextLine(); System.out.println("Enter String-2"); String s2=sc.nextLine(); System.out.println("Length of String-1 is:"+s1.length()); System.out.println("Length of String-2 is:"+s2.length()); System.out.println("String-1 in LowerCase-"+s1.toLowerCase()); System.out.println("String-2 in UpperCase-"+s2.toUpperCase()); System.out.println("Check s1 equals s2-"+s1.equals(s2)); System.out.println("Comapre s1 & s2:"+s1.compareTo(s2)); System.out.println("Chatracter of String at postion 0 is:"+s1.charAt(0)); String s3=s1.concat(s2); System.out.println("Concatened String is:-"+s3); System.out.println("Substring is:"+s3.substring(0,11)); System.out.println("After Replacing o with a-"+ s3.replace('o','a')); System.out.println("Index of Character N is:"+s3.indexOf('n')); } } /* Java Program to input 10 Name and bubble sort them */ import java.util.*; public class test { public static void main(String args[]) { String str="Sky Is Blue",word=""; str= str+"";//Append space for extracting last word char ch= ' '; int swl=str.length(); // Let Smallest Word Length is Entire Sentence Length int mwl=0; // Let Largest Word Length is Zero String sword="", lword=""; for(int i=0;i<str.length();i++) { ch=str.charAt(i); if(ch!=' ') { word+=ch;// Build Word from character Excluding spaces } else { if(word.length()< swl)//Checking for Smallest Word Length { swl=word.length(); sword=word; } if(word.length()> mwl) { mwl=word.length(); lword=word; } word="";// Emptying variable for forming next word } } System.out.println("Smallest Word is " + sword + " having length=" + swl); System.out.println("Largest Word is " + lword + " having length=" + mwl); } } /* Java Program to Count Vowels and Consonant in a Sentence */ import java.util.*; public class countconstvowel { public static void main(String args[]) { int vowelcount=0,constcount=0; String s="Noteshacker is an amazing website "; s=s.toLowerCase(); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'|| s.charAt(i)=='u') { vowelcount++; } else if(s.charAt(i)>='a' && s.charAt(i)<='z') { constcount++; } } System.out.println("Number of Vowels in the Sentences is:"+ vowelcount); System.out.println("Number of Consonant in the Sentences is:"+ constcount); } } /* Java Program to Count No. of words in a Sentence */ public class CountWords { public static void main(String[] args) { String str = "Sky is Blue"; int count = 1; for (int i = 0; i < str.length() - 1; i++) { if ((str.charAt(i) == ' ') && (str.charAt(i + 1) != ' ')) { count++; } } System.out.println("Number of words in the string : " + count); } } /*Program to accept a string. Convert the string to uppercase and Count the number of double letter sequences that exist in the string.*/ import java.util.Scanner; public class StringOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a String: "); String input = scanner.nextLine(); input = input.toUpperCase(); int count = 0; for (int i = 1; i < input.length(); i++) { if (input.charAt(i) == input.charAt(i - 1)) { count++; } } System.out.println(count); } } } /* Java Program to input 10 Name and bubble sort them */ import java.util.Scanner; class BubbleSort { public static void main(String []args) { int i, j,n=10; String temp; Scanner in = new Scanner(System.in); System.out.println("Input Ten Names in Random Order"); String names[] = new String[n]; for (i = 0; i < n; i++) names[i] = in.nextLine(); for (i = 0; i < n; i++) { for (j = 0; j < n - i - 1; j++) { if (names[j].compareToIgnoreCase(names[j+1]) >0 ) { temp = names[j]; names[j] = names[j+1]; names[j+1] = temp; } } } System.out.println("Sorted Names :"); for (i = 0; i < 10; i++) System.out.println(names[i]); } } int a=6,b=5; a += a++ % b++ *a + b++* --b; a += a++ % b++ *a + b++* --b => a = a + (a++ % b++ *a + b++* --b) => a = 6 + (6 % 5 * 7 + 6 * 6) // % and * will be applied first due to higher precedence => a = 6 + (7 + 36) => a = 49 (c) Give the output of the snippet: int a=10,b=12; if(a==10&&b<=12) a--; else ++b; System.out.println(a + "and" +b); (b) Write a program to display the given pattern: 11111 3333 555 77 9 public class KboatPattern { public static void main(String args[]) { for (int i = 5, term = 1; i >= 0; i--, term += 2) { for (int j = 1; j <= i; j++) { System.out.print(term + " "); } System.out.println(); } } } (b) Write a program to display the given pattern: 3 56 8 9 10 12 13 14 15 public class KboatPattern { public static void main(String args[]) { int t = 2; for (int i = 1, j = 1; i <= 4; i++, j++) { t += i; for (int k = t, l = 1; l <= j; k++, l++) System.out.print(k + " "); System.out.println(); } } } import java.util.Scanner; public class KboatDigitSum { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); int n = in.nextInt(); int sum = 0, count = 0; while (n != 0) { int d = n % 10; sum += d; count++; n /= 10; } System.out.println("Sum of digits = " + sum); System.out.println("Number of digits = " + count); } } Primitive Data Types A primitive data type specifies the size and type of variable values, and it has no additional methods. There are eight primitive data types in Java: Data Type byte short int long Size Description 1 byte 2 bytes 4 bytes 8 bytes float double boolean char 4 bytes 8 bytes 1 bit 2 bytes Stores whole numbers from -128 to 127 Stores whole numbers from -32,768 to 32,767 Stores whole numbers from -2,147,483,648 to 2,147,483,647 Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits Stores fractional numbers. Sufficient for storing 15 decimal digits Stores true or false values Stores a single character/letter or ASCII values Wrapper Class Byte Short Integer Long Float Double Boolean Character Non-Primitive Data Types Non-primitive data types are called reference types because they refer to objects. The main difference between primitive and non-primitive data types are: Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String). Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot. A primitive type has always a value, while non-primitive types can be null. A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter. The size of a primitive type depends on the data type, while non-primitive types have all the same size. Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc. Use of Wrapper classes in Java Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes. o Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value. o Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes. o Synchronization: Java synchronization works with objects in Multithreading. o java.util package: The java.util package provides the utility classes to deal with objects. o Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only. The eight classes of the java.lang package are known as wrapper classes in Java. public class out { public static void main() { //Converting int into Integer int p=20; Integer w=Integer.valueOf(p);//converting int into Integer explicitly Integer x=p;//autoboxing, now compiler will write Integer.valueOf(a) internally System.out.println(p+" "+w+" "+x); //Converting Integer to int Integer q=new Integer(3); int y=q.intValue();//converting Integer to int explicitly int z=q;//unboxing, now compiler will write a.intValue() internally System.out.println(q+" "+y+" "+z); } } The Java Runtime Environment (JRE) contains Java Virtual Machine, class libraries and browser plugins for applet and application execution. Q. What is a Servlet? The servlet is a Java programming language class used to process client requests and generate dynamic web content. Servlets are mostly used to process or store data submitted by an HTML form, provide dynamic content and manage state information that does not exist in the stateless HTTP protocol. JDBC is an abstraction layer that allows users to choose between databases. JDBC enables developers to write database applications in Java without having to concern themselves with the underlying details of a particular database.