JAVA UNIT 2(Part 1)
BY: SURBHI SAROHA
SYLLABUS
 Java Lang package
 Simple type wrappers
 Number,Double,float,byte,short,int,long,character,Boolean,process and void.
 The Maths class.
 Java Utility and Collection Classes.
Cont….
 Arrays as collections
 Algorithms
 Wrappers as implementations
 Extending the abstract implementation legacy collections
 Framework classes
 Traversing collections with enumeration’s
Java Lang package
 Provides classes that are fundamental to the design of the Java programming language.
 The most important classes are Object, which is the root of the class hierarchy, and Class, instances
of which represent classes at run time.
 Following are the Important Classes in Java.lang package :
 Boolean: The Boolean class wraps a value of the primitive type boolean in an object.
 Byte: The Byte class wraps a value of primitive type byte in an object.
 Character – Set 1, Set 2: The Character class wraps a value of the primitive type char in an object.
 Character.Subset: Instances of this class represent particular subsets of the Unicode character set.
 Character.UnicodeBlock: A family of character subsets representing the character blocks in the
Unicode specification.
 Class – Set 1, Set 2 : Instances of the class Class represent classes and interfaces in a running Java
application.
Cont…
 ClassLoader: A class loader is an object that is responsible for loading classes.
 ClassValue: Lazily associate a computed value with (potentially) every type.
 Compiler: The Compiler class is provided to support Java-to-native-code compilers and related
services.
 Double: The Double class wraps a value of the primitive type double in an object.
 Enum: This is the common base class of all Java language enumeration types.
 Float: The Float class wraps a value of primitive type float in an object.
 InheritableThreadLocal: This class extends ThreadLocal to provide inheritance of values from
parent thread to child thread: when a child thread is created, the child receives initial values for all
inheritable thread-local variables for which the parent has values.
 Integer :The Integer class wraps a value of the primitive type int in an object.
 Long: The Long class wraps a value of the primitive type long in an object.
Cont…
 Math – Set 1, Set 2: The class Math contains methods for performing basic
numeric operations such as the elementary exponential, logarithm, square root,
and trigonometric functions.
 Number: The abstract class Number is the superclass of classes BigDecimal,
BigInteger, Byte, Double, Float, Integer, Long, and Short.
 Object: Class Object is the root of the class hierarchy.
 Package: Package objects contain version information about the implementation
and specification of a Java package.
 Process: The ProcessBuilder.start() and Runtime.exec methods create a native
process and return an instance of a subclass of Process that can be used to control
the process and obtain information about it.
 ProcessBuilder: This class is used to create operating system processes.
Cont…
 Math – Set 1, Set 2: The class Math contains methods for performing basic
numeric operations such as the elementary exponential, logarithm, square root,
and trigonometric functions.
 Number: The abstract class Number is the superclass of classes BigDecimal,
BigInteger, Byte, Double, Float, Integer, Long, and Short.
 Object: Class Object is the root of the class hierarchy.
 Package: Package objects contain version information about the implementation
and specification of a Java package.
 Process: The ProcessBuilder.start() and Runtime.exec methods create a native
process and return an instance of a subclass of Process that can be used to control
the process and obtain information about it.
 ProcessBuilder: This class is used to create operating system processes.
Cont…
 StringBuffer: A thread-safe, mutable sequence of characters.
 StringBuilder: A mutable sequence of characters.
 System: The System class contains several useful class fields and methods.
 Thread: A thread is a thread of execution in a program.
 ThreadGroup: A thread group represents a set of threads.
 ThreadLocal: This class provides thread-local variables.
 Throwable: The Throwable class is the superclass of all errors and exceptions in the
Java language.
 Void: The Void class is an uninstantiable placeholder class to hold a reference to
the Class object representing the Java keyword void.
Simple type wrappers
 A Wrapper class is a class whose object wraps or contains primitive data types.
 When we create an object to a wrapper class, it contains a field and in this field, we
can store primitive data types.
 In other words, we can wrap a primitive value into a wrapper class object.
 Need of Wrapper Classes
 They convert primitive data types into objects. Objects are needed if we wish to
modify the arguments passed into a method (because primitive types are passed
by value).
 The classes in java.util package handles only objects and hence wrapper classes
help in this case also.
Cont….
 Data structures in the Collection framework, such as ArrayList and Vector, store
only objects (reference types) and not primitive types.
 An object is needed to support synchronization in multithreading.
 Primitive Data types and their Corresponding Wrapper class
Number,Double,float,byte,short,int,
long,character,Boolean,process and void.
 int myNum = 5; // Integer (whole number)
 float myFloatNum = 5.99f; // Floating point number
 char myLetter = 'D'; // Character
 boolean myBool = true; // Boolean
 String myText = "Hello"; // String
 Data types are divided into two groups:
 Primitive data types - includes byte, short, int, long, float, double, boolean and char
 Non-primitive data types - such as String, Arrays and Classes
There are eight primitive data types in Java:
 Data Type Size Description
 byte 1 byte Stores whole numbers from -128 to 127
 short 2 bytes Stores whole numbers from -32,768 to 32,767
 int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
 long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
 float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
 double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
 boolean 1 bit Stores true or false values
 char 2 bytes Stores a single character/letter or ASCII values
The Maths class
 Java Math class provides several methods to work on math calculations like min(), max(), avg(), sin(),
cos(), tan(), round(), ceil(), floor(), abs() etc.
 Example 1
 public class JavaMathExample1
 {
 public static void main(String[] args)
 {
 double x = 28;
 double y = 4;

 // return the maximum of two numbers
 System.out.println("Maximum number of x and y is: " +Math.max(x, y));
Cont…
 // return the square root of y
 System.out.println("Square root of y is: " + Math.sqrt(y));

 //returns 28 power of 4 i.e. 28*28*28*28
 System.out.println("Power of x and y is: " + Math.pow(x, y));

 // return the logarithm of given value
 System.out.println("Logarithm of x is: " + Math.log(x));
 System.out.println("Logarithm of y is: " + Math.log(y));

 // return the logarithm of given value when base is 10
 System.out.println("log10 of x is: " + Math.log10(x));
 System.out.println("log10 of y is: " + Math.log10(y));
Cont….
 // return the log of x + 1
 System.out.println("log1p of x is: " +Math.log1p(x));

 // return a power of 2
 System.out.println("exp of a is: " +Math.exp(x));

 // return (a power of 2)-1
 System.out.println("expm1 of a is: " +Math.expm1(x));
 }
 }
Java Utility and Collection Classes.
 Collections class in java is a useful utility class to work with collections in java.
 The java.util.Collections class directly extends the Object class and exclusively
consists of the static methods that operate on Collections or return them.
 Collections Class in java
 Collections class contains polymorphic algorithms that operate on collections and
“wrappers” — which return a new collection backed by a specified collection. It is a
member of Java Collections Framework.
 Collections class fields
 Collections class contains 3 fields: EMPTY_LIST, EMPTY_SET, EMPTY_MAP, which can
be used to get immutable empty List, Map and Set respectively.
Example
 package utility;
 import java.util.Collections;
 import java.util.ArrayList;
 import java.util.List;
 public class CollectionsDemo {
 public static void main(String[] args) {
 List<String>student<String>List = new ArrayList();
 studentList.add("Neeraj");
 studentList.add("Mahesh");
 studentList.add("Armaan");

Cont….
 studentList.add("Preeti");
 studentList.add("Sanjay");
 studentList.add("Neeraj");
 studentList.add("Zahir");
 System.out.println("Original List " + studentList);
 Collections.sort(studentList);
 System.out.println("Sorted alphabetically List " + studentList);
 Collections.reverse(studentList);
 }
Cont….
 System.out.println("Reverse List " + studentList);
 Collections.shuffle(studentList);
 System.out.println("Shuffled List " + studentList);
 System.out.println("Checking occurance of Neeraj: "
 + Collections.frequency(studentList, "Neeraj"));
THANK YOU 

Java Unit 2(Part 1)

  • 1.
    JAVA UNIT 2(Part1) BY: SURBHI SAROHA
  • 2.
    SYLLABUS  Java Langpackage  Simple type wrappers  Number,Double,float,byte,short,int,long,character,Boolean,process and void.  The Maths class.  Java Utility and Collection Classes.
  • 3.
    Cont….  Arrays ascollections  Algorithms  Wrappers as implementations  Extending the abstract implementation legacy collections  Framework classes  Traversing collections with enumeration’s
  • 4.
    Java Lang package Provides classes that are fundamental to the design of the Java programming language.  The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time.  Following are the Important Classes in Java.lang package :  Boolean: The Boolean class wraps a value of the primitive type boolean in an object.  Byte: The Byte class wraps a value of primitive type byte in an object.  Character – Set 1, Set 2: The Character class wraps a value of the primitive type char in an object.  Character.Subset: Instances of this class represent particular subsets of the Unicode character set.  Character.UnicodeBlock: A family of character subsets representing the character blocks in the Unicode specification.  Class – Set 1, Set 2 : Instances of the class Class represent classes and interfaces in a running Java application.
  • 5.
    Cont…  ClassLoader: Aclass loader is an object that is responsible for loading classes.  ClassValue: Lazily associate a computed value with (potentially) every type.  Compiler: The Compiler class is provided to support Java-to-native-code compilers and related services.  Double: The Double class wraps a value of the primitive type double in an object.  Enum: This is the common base class of all Java language enumeration types.  Float: The Float class wraps a value of primitive type float in an object.  InheritableThreadLocal: This class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values.  Integer :The Integer class wraps a value of the primitive type int in an object.  Long: The Long class wraps a value of the primitive type long in an object.
  • 6.
    Cont…  Math –Set 1, Set 2: The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.  Number: The abstract class Number is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.  Object: Class Object is the root of the class hierarchy.  Package: Package objects contain version information about the implementation and specification of a Java package.  Process: The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it.  ProcessBuilder: This class is used to create operating system processes.
  • 7.
    Cont…  Math –Set 1, Set 2: The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.  Number: The abstract class Number is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.  Object: Class Object is the root of the class hierarchy.  Package: Package objects contain version information about the implementation and specification of a Java package.  Process: The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it.  ProcessBuilder: This class is used to create operating system processes.
  • 8.
    Cont…  StringBuffer: Athread-safe, mutable sequence of characters.  StringBuilder: A mutable sequence of characters.  System: The System class contains several useful class fields and methods.  Thread: A thread is a thread of execution in a program.  ThreadGroup: A thread group represents a set of threads.  ThreadLocal: This class provides thread-local variables.  Throwable: The Throwable class is the superclass of all errors and exceptions in the Java language.  Void: The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.
  • 9.
    Simple type wrappers A Wrapper class is a class whose object wraps or contains primitive data types.  When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types.  In other words, we can wrap a primitive value into a wrapper class object.  Need of Wrapper Classes  They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).  The classes in java.util package handles only objects and hence wrapper classes help in this case also.
  • 10.
    Cont….  Data structuresin the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.  An object is needed to support synchronization in multithreading.  Primitive Data types and their Corresponding Wrapper class
  • 11.
    Number,Double,float,byte,short,int, long,character,Boolean,process and void. int myNum = 5; // Integer (whole number)  float myFloatNum = 5.99f; // Floating point number  char myLetter = 'D'; // Character  boolean myBool = true; // Boolean  String myText = "Hello"; // String  Data types are divided into two groups:  Primitive data types - includes byte, short, int, long, float, double, boolean and char  Non-primitive data types - such as String, Arrays and Classes
  • 12.
    There are eightprimitive data types in Java:  Data Type Size Description  byte 1 byte Stores whole numbers from -128 to 127  short 2 bytes Stores whole numbers from -32,768 to 32,767  int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647  long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807  float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits  double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits  boolean 1 bit Stores true or false values  char 2 bytes Stores a single character/letter or ASCII values
  • 13.
    The Maths class Java Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.  Example 1  public class JavaMathExample1  {  public static void main(String[] args)  {  double x = 28;  double y = 4;   // return the maximum of two numbers  System.out.println("Maximum number of x and y is: " +Math.max(x, y));
  • 14.
    Cont…  // returnthe square root of y  System.out.println("Square root of y is: " + Math.sqrt(y));   //returns 28 power of 4 i.e. 28*28*28*28  System.out.println("Power of x and y is: " + Math.pow(x, y));   // return the logarithm of given value  System.out.println("Logarithm of x is: " + Math.log(x));  System.out.println("Logarithm of y is: " + Math.log(y));   // return the logarithm of given value when base is 10  System.out.println("log10 of x is: " + Math.log10(x));  System.out.println("log10 of y is: " + Math.log10(y));
  • 15.
    Cont….  // returnthe log of x + 1  System.out.println("log1p of x is: " +Math.log1p(x));   // return a power of 2  System.out.println("exp of a is: " +Math.exp(x));   // return (a power of 2)-1  System.out.println("expm1 of a is: " +Math.expm1(x));  }  }
  • 16.
    Java Utility andCollection Classes.  Collections class in java is a useful utility class to work with collections in java.  The java.util.Collections class directly extends the Object class and exclusively consists of the static methods that operate on Collections or return them.  Collections Class in java  Collections class contains polymorphic algorithms that operate on collections and “wrappers” — which return a new collection backed by a specified collection. It is a member of Java Collections Framework.  Collections class fields  Collections class contains 3 fields: EMPTY_LIST, EMPTY_SET, EMPTY_MAP, which can be used to get immutable empty List, Map and Set respectively.
  • 17.
    Example  package utility; import java.util.Collections;  import java.util.ArrayList;  import java.util.List;  public class CollectionsDemo {  public static void main(String[] args) {  List<String>student<String>List = new ArrayList();  studentList.add("Neeraj");  studentList.add("Mahesh");  studentList.add("Armaan"); 
  • 18.
    Cont….  studentList.add("Preeti");  studentList.add("Sanjay"); studentList.add("Neeraj");  studentList.add("Zahir");  System.out.println("Original List " + studentList);  Collections.sort(studentList);  System.out.println("Sorted alphabetically List " + studentList);  Collections.reverse(studentList);  }
  • 19.
    Cont….  System.out.println("Reverse List" + studentList);  Collections.shuffle(studentList);  System.out.println("Shuffled List " + studentList);  System.out.println("Checking occurance of Neeraj: "  + Collections.frequency(studentList, "Neeraj"));
  • 20.