Understanding Core Packages Er. Arjun Sharma Poudel Packages Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for : • Preventing naming conflicts • Making searching/locating and usage of classes, interfaces, enumerations and annotations easier • Providing controlled access: protected and default have package level access control. A protected member is accessible by classes in the same package and its subclasses. A default member (without any access specifier) is accessible by classes in the same package only. • Packages can be considered as data encapsulation (or data-hiding). java.lang: Contains language support classes(e.g. classed which defines primitive data types, math operations). This package is automatically imported. Er. Arjun Sharma Poudel Programs for Java.lang.Math class public class MathDemo { public static void main(String[] args) { System.out.println("Maximum number is:\t"+Math.max(6,7)); System.out.println("Square root of y is:\t"+Math.sqrt(25)); System.out.println("Power of x and y is:\t"+Math.pow(2,3)); System.out.println("Rounding value of:\t"+Math.round(35.4)); System.out.println("Value of cos 0\t"+Math.cos(0)); System.out.println("Ceiling Value of\t:"+Math.ceil(3.1)); System.out.println("Value of PI\t:"+Math.PI); System.out.println("Log value of:\t"+Math.log(90)); } } Er. Arjun Sharma Poudel Wrapper Classes and Associated methods: Wrapper classes are objects encapsulating primitive Java types. We learned that Java programming is all about objects but the eight primitive data types byte, short, int, long, float, double, char and boolean are not objects. To solve this problem Wrapper classes are used for converting primitive data types into objects. Why we need wrapper class in Java? The primitive data types are not objects so they do not belong to any class. While storing in data structures which support only objects, it is required to convert the primitive type to object first which we can do by using wrapper classes. Data structures in the Collection framework such as ArrayList and Vector store only the objects (reference types) and not the primitive types. Er. Arjun Sharma Poudel Primitive Data type Wrapper Classes boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double Er. Arjun Sharma Poudel AutoBoxing : Converting a primitive value into an object of the corresponding wrapper class is called autoboxing. For example, converting int to Integer class UnBoxing : Converting an object of a wrapper type to its corresponding primitive value is called unboxing. For example conversion of Integer to int. // Auto-unboxing of Character Programs: char ch1 = ch; public class Demo1 { System.out.println("Value of ch: " + ch); public static void main(String[] args) { System.out.println("Value of ch1: " + ch1); Integer i = new Integer(10); } // unboxing the Object } int i1 = i; System.out.println("Value of i: " + i); System.out.println("Value of i1: " + i1); //Autoboxing of char Character ch= 'a'; Er. Arjun Sharma Poudel Methods of Wrapper classes valueOf() : to create Wrapper object for given primitive or String. Integer I = Integer.valueOf(10); Double D = Double.valueOf(10.5); Character C = Character.valueOf('a’); parseXxx() : We can use parseXxx() methods to convert String to primitive. int i = Integer.parseInt("10"); double d = Double.parseDouble("10.5"); boolean b = Boolean.parseBoolean("true"); toString() :We can use toString() method to convert Wrapper object or primitive to String Integer I = new Integer(10); String s = I.toString(); String s2 = Integer.toString(10); String s1 = Character.toString('a'); Er. Arjun Sharma Poudel Programs: public class PracticeW { public static void main(String[] args) { Integer I = 130; System.out.println(I.byteValue()); System.out.println(I.shortValue()); System.out.println(I.intValue()); System.out.println(I.longValue()); System.out.println(I.floatValue()); System.out.println(I.doubleValue()); } } Er. Arjun Sharma Poudel Programs: public class AutoBoxingTest { public static void main(String[] args) { int i=10; Integer iobj=new Integer(i); System.out.println(iobj); System.out.println(iobj3+"\tis greater"); } String s=new String("45"); System.out.println(Integer.toHexString(45)); String s1="54"; System.out.println(Integer.toBinaryString(45)); Integer iobj2=Integer.valueOf(s); System.out.println(Integer.toOctalString(45)); Integer iobj3=Integer.valueOf(s1); System.out.println(iobj2); if(iobj2>iobj3){ } } System.out.println(iobj2+"\tis greater"); } else{ Er. Arjun Sharma Poudel Programs: public class UnboxingTest { public static void main(String[] args) { Integer I1=new Integer(40); Integer I2=new Integer(50); int i1=I1.intValue(); int i2=I2.intValue(); String s=I2.toString(); Integer i3=Integer.valueOf("45"); int x=Integer.parseInt("32"); System.out.println("i1\t"+i1+"\ni2\t"+i2+"\ns"+s+"\ni3"+i3+"\nX\t"+x); } } Er. Arjun Sharma Poudel Core Classes: Enumeration: Enumeration means a list of named constant. In Java, enumeration defines a class type. It is created using enum keyword. Each enumeration constant is public, static and final by default. Even though enumeration defines a class type and have constructors, you do not instantiate an enum using new. Enumeration variables are used and declared in much a same way as you do a primitive variable. By using enumeration you can enumerate(obtain one at a time) the elements in a collection of objects Program enum WeekDays{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } public class EnumTest { public static void main(String[] args) { WeekDays arr[]=WeekDays.values(); for (WeekDays wk:arr) { System.out.println(wk); } } } Er. Arjun Sharma Poudel Core Classes: Stack: import java.util.Stack; public class StackTest { public static void main(String[] args) { // Create a stack of integers Stack<Integer> s = new Stack<>(); // Insert data into the stack s.push(5); s.push(2); s.push(6); s.push(9); s.push(8); s.push(9); Er. Arjun Sharma Poudel // Delete data from the stack s.pop(); // Show the last item added in the stack System.out.println("Last item added in stack: " + s.peek()); // Search for the object 9 and print the index System.out.println("Index of object 9: " + s.search(9)); // Print the stack System.out.println("Stack content: " + s); } } Er. Arjun Sharma Poudel Core Classes: Random Number Generation(java.util.Random): Random class is used to generate random numbers in java. import java.util.Random; public class RandomTest { public static void main(String[] args) { // Create a Random object Random r = new Random(); // Generate a random integer between 0 (inclusive) and 1000 (exclusive) int x = r.nextInt(1000); System.out.println("Random integer between 0 and 999: " + x); // Generate a random double between 0.0 (inclusive) and 1.0 (exclusive) double y = r.nextDouble(); System.out.println("Random double between 0.0 and 1.0: " + y); // Generate a random integer int z = r.nextInt(); System.out.println("Random integer: " + z); } } Er. Arjun Sharma Poudel Hash Table : Core Classes: Hash Table maps key to values. Hashtable is similar to HashMap except it is synchronized. if there is a need of thread-safe operation then Hashtable can be used as all its methods are synchronized but it’s a legacy class and should be avoided as there is nothing about it, which cannot be done by HashMap.. Program Er. Arjun Sharma Poudel import java.util.Enumeration; import java.util.Hashtable; public class HashTableTest { public static void main(String[] args) { // Create a Hashtable Hashtable<String, String> ht = new Hashtable<>(); // Add entries to the Hashtable ht.put("key 1", "Romeo"); ht.put("Key 2", "Peter"); // Get the keys from the Hashtable Enumeration<String> name = ht.keys(); // Iterate through the keys and print key-value pairs while (name.hasMoreElements()) { String key = name.nextElement(); System.out.println("Key: " + key + "\t Value: " + ht.get(key)); } } } Er. Arjun Sharma Poudel Java.lang.math public class MathDemo { public static void main(String[] args) { System.out.println("Maximmum number is:\t"+Math.max(6,7)); System.out.println("Square root of y is:\t"+Math.sqrt(25)); System.out.println("Power of x and y is:\t"+Math.pow(2,3)); System.out.println("Rounding value of:\t"+Math.round(35.4)); System.out.println("Value of cos 0\t"+Math.cos(0)); System.out.println("Ceiling Value of\t:"+Math.ceil(3.1)); System.out.println("Value of PI\t:"+Math.PI); System.out.println("Log value of:\t"+Math.log(90)); } } Er. Arjun Sharma Poudel THIK XA? Er. Arjun Sharma Poudel