Download presentation
Presentation is loading. Please wait.
Published byCora McGee Modified over 9 years ago
1
Package and Some Classes Declaration of Package Usage of Package Package of Java Language
2
What is Package ? q Way to group a related class and interface into one unit q To resolve the name conflicts between class names
3
Declaration of Package q Declare at the beginning of source file [Print1.java], [Print2.java] package PackageName ; MyTest Prints Print1.class Print2.class
4
Usage of Package q Use of absolute path name 2 Declare to describe the full package name 2 To resolve conflicts between class names [AbsolutePath.java] MyTest.Prints.Print1 m1; MyTest.Prints.Print2 m2; MyTest.Prints.Print1 m1; MyTest.Prints.Print2 m2;
5
Use of import Statement q Include all the classes in package 2 import packageName.*; [ImportTest.java] import packageName.className ; import MyTest.Prints.*;
6
java.lang Package q Basic class to enlarge the function of Java q Provide classes according to primitive type (wrapper class) 2 Integer class, Double class,...
7
java.lang Class Object Number Integer Long Float Double Character System String StringBuffer Boolean Wrapper Class
8
Object Class q q Super class of all classes q q All classes are to inherit the methods of Object class q q Method 2 Object clone() : To copy the object equally 2 boolean equals(Object obj) : Compare the object as parameter with the object which calls this method 2 int hashCode() : Calculate the hash code value of the object 2 Class getClass() : To get Class object corresponding to object 2 String toString() : Convert the object into string [Ex 8.5] 2 wait(), notify() : To control the status of thread
9
Wrapper Class q Classes correspond to primitive type q int -> Integer, char -> Character, double -> Double q Reasons Role of home for constants and methods for each type 2 When we need to deal with primitive type as object
10
Number Class q Abstract Class Super Class of Integer, Long, Float, Double q q Method 2 abstract int intValue() : Convert into int type 2 abstract long longValue() : Convert into long type 2 abstract float floatValue() : Convert into float type 2 abstract double doubleValue() : Convert into double type
11
Integer Class q Constant 2 public static final int MAX_VALUE = 2147483647 2 public static final int MIN_VALUE = -2147483648 q Method 2 static int parseInt(String s) : Convert a Number in String into int type 2 static int parseInt(String s, int radix) : Convert a number in String into int type with radix 2 static String toBinaryString(int i) : Convert into binary string form 2 static String toHexString(int i) : Convert into hexadecimal string form
12
Integer Class [IntegerClass.java] Interger.parseInt(s); Interger.toBinaryString(i);... Interger.parseInt(s); Interger.toBinaryString(i);... As it is static method... As it is static method...
13
Double Class q Constant 2 public static final double MAX_VALUE =1.79769313486231570e+308 2 public static final double MIN_VALUE = 4.94065645841246544e-308 2 public static final double NaN = 0.0 / 0.0 2 public static final double NEGATIVE_INFINITY = -1.0 / 0.0 2 public static final double POSITIVE_INFINITY = 1.0 / 0.0
14
Double Class q the parameter is NaN or not. 2 static boolean isInfinite(double v) : Check whether the parameter is infinite or not. 2 static Double valueOf(String s) : Method 2 static long doubleToLongBits(double value) : Convert the bits represented by double type into long type bit pattern 2 static boolean isNaN(double v) : Check whether Convert String into Double type
15
Boolean Class q Constant 2 public static final Boolean TRUE = new Boolean(true) 2 public static final Boolean FALSE = new Boolean(false) q Method 2 Boolean(boolean b) : Constructor to create boolean object receiving the initial value b 2 Boolean(String s) : Constructor to receive the string value "true“ or "false“ 2 boolean booleanValue() : Return the boolean value of object 2 static boolean getBoolean(String name) : Return the boolean value of system attribute 2 static Boolean valueOf(String s) : Return the Boolean value correspond to string s
16
Character Class q Constant 2 public static final int MAX_RADIX = 36 2 public static final char MAX_VALUE =‘\ffff’ 2 public static final int MIN_RADIX = 2 2 public static final char MIN_VALUE =’\0000’ q Method 2 Character(char value) : Constructor to initialize the object as value 2 char charValue() : Convert into char type 2 static boolean isDigit(char ch) : Test whether is digit? 2 static boolean isLetter(char ch) : Test whether is letter? 2 static boolean isLetterOrDigit(char ch) : Return when it is letter or digit. [CharacterClass.java]
17
String Class q Method 2 char charAt(int index) : Return the character at specific position in string 2 int length() : Return the length of string 2 String toUpperCase() : Convert the string into upper character 2 String toLowerCase() : Convert the string into lower character 2 String trim() : Remove the white space before/after string 2 String substring(int beginIndex) : Make the sub string from beginIndex to the end of the string
18
StringBuffer Class q Provide the string sequence operation q Method 2 StringBuffer append(Type obj) : Append the obj(to be changed into string) to string 2 StringBuffer insert(int offset, Type obj) : Insert obj(to be changed into string) into specified position ObjectStringchar[] booleancharint longfloatdouble ObjectStringchar[] booleancharint longfloatdouble
19
StringBuffer Class String now = new java.util.Date().toString(); StringBuffer strbuf = new StringBuffer(now); strbuf.append(" : ").append(now.length).append('.'); System.out.println(strbuf.toString()); String now = new java.util.Date().toString(); StringBuffer strbuf = new StringBuffer(now); strbuf.append(" : ").append(now.length).append('.'); System.out.println(strbuf.toString());
20
System Class q q Class for Java Virtual Machine and the control and security for OS system q q Define the standard input and output q q Field 2 public static InputStream in : Stream to read data from standard input device 2 public static PrintStream out : Stream to output data to standard output device 2 public static PrintStream err : Standard Error Stream to output error message
21
Java.util.vectorJava.util.vector q q We will be creating an object of Vector class and performs various operation like adding, removing etc. q q Vector class extendsAbstractList and implements List, RandomAccess, Cloneable, Serializable. q q The size of a vector increase and decrease according to the program. q q Vector is synchronized.
22
Methods in Vector q q add(Object o): It adds the element in the end of the Vector q q size(): It gives the number of element in the vector. q q elementAt(int index): It returns the element at the specified index. q q firstElement(): It returns the first element of the vector. q q lastElement(): It returns last element. q q removeElementAt(int index): It deletes the element from the given index.
23
Methods in Vector class q q elements(): It returns an enumeration of the element q q In this example we have also used Enumeration interface to retrieve the value of a vector. Enumerationinterface has two methods. q q hasMoreElements(): It checks if this enumeration contains more elements or not. q q nextElement(): It checks the next element of the enumeration.
24
import java.util.*; public class VectorDemo{ public static void main(String[] args){ Vector vector = new Vector (); int primitiveType = 10; Integer wrapperType = new Integer(20); String str = "tapan joshi"; vector.add(primitiveType); vector.add(wrapperType); vector.add(str); vector.add(2, new Integer(30));
25
System.out.println("the elements of vector: " + vector); System.out.println("The size of vector are: " + vector.size()); System.out.println("The elements at position 2 is: " + vector.elementAt(2)); System.out.println("The first element of vector is: " + vector.firstElement()); System.out.println("The last element of vector is: " + vector.lastElement()); vector.removeElementAt(2); Enumeration e=vector.elements(); System.out.println("The elements of vector: " + vector); while(e.hasMoreElements()){ System.out.println("The elements are: " + e.nextElement()); } } }
26
Example on Stack class import java.util.*; public class StackDemo{ public static void main(String[] args) { Stack stack=new Stack(); stack.push(new Integer(10)); stack.push("a"); System.out.println("The contents of Stack is" + stack); System.out.println("The size of an Stack is" + stack.size()); System.out.println("The number poped out is" + stack.pop()); System.out.println("The number poped out is " + stack.pop()); //System.out.println("The number poped out is" + stack.pop()); System.out.println("The contents of stack is" + stack); System.out.println("The size of an stack is" + stack.size()); } }
27
Example on Scanner class import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Read string input for username System.out.print("Username: "); String username = scanner.nextLine(); // Read string input for password System.out.print("Password: "); String password = scanner.nextLine();
28
// Read an integer input for another challenge System.out.print("What is 2 + 2: "); int result = scanner.nextInt(); if (username.equals("admin") && password.equals("secret") && result == 4) { System.out.println("Welcome to Java Application"); } else { System.out.println("Invalid username or password, access denied!"); }}}
29
Example on Date Class import java.util.*; public class DateDemo{ public static void main(String[] args) { Date d=new Date(); System.out.println("Today date is "+ d); } }
30
JAVA.IO
31
Example on File import java.io.*; class FileWrite { public static void main(String args[]) { try{ // Create file FileWriter fstream = new FileWriter("out.txt",true); BufferedWriter out = new BufferedWriter(fstream); out.write("Hello Java"); //Close the output stream out.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }
32
java.io.Reader class
33
BufferedReader class Method Return Type Description read( ) int Reads a single character read(char[] cbuf, int off, int len) int Read characters into a portion of an array. readLine( ) String Read a line of text. A line is considered to be terminated by ('\n'). close( ) void Closes the opened stream.
34
import java.io.*; public class ReadStandardIO{ public static void main(String[] args) throws IOException{ InputStreamReader inp = new InputStreamReader(System.in) BufferedReader br = new BufferedReader(inp); System.out.println("Enter text : "); String str = in.readLine(); System.out.println("You entered String : "); System.out.println(str); } }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.