Class 37
Class 37
========
A package is a collection of classes,interfaces,enums and annotations.
1) Predefined packages
--------------------
Built-In packages are called predefined packages.
ex:
java.lang
java.io
java.util
java.util.stream
java.time
and etc.
2) Userdefined packages
-----------------------
A package which is created by the user based on the application requirement is
called predefined package.
syntax:
-----
package <package_name>;
ex:
package com.google.www;
ex:
----
package com.ihub.www;
import java.util.Calendar;
class Test
{
public static void main(String[] args)
{
Calendar c=Calendar.getInstance();
int h=c.get(Calendar.HOUR_OF_DAY);
if(h<12)
System.out.println("Good Morning");
else if(h<16)
System.out.println("Good Afternoon");
else if(h<20)
System.out.println("Good Evening");
else
System.out.println("Good Night");
}
}
java com.ihub.www.Test
|
package name
Enum
=====
Enum is a group of named constants.
Using enum we can create our own datatype called enumerated datatype.
syntax:
------
enum type_name
{
value1,value2,.....,valueN
}
ex:
ex:
---
enum Months
{
JAN,FEB,MAR
}
class Test
{
public static void main(String[] args)
{
Months m=Months.FEB;
switch(m)
{
case JAN: System.out.println("January"); break;
}
}
java.lang.Enum class
--------------------
The power to enum will be inherited from java.lang.Enum class.
1) values()
---------
It is used to read set of constants from enum.
2) ordinal()
-----------
It is used to display ordinal number.
ex:
---
enum Week
{
MON,TUE,WED,THU,FRI,SAT,SUN
}
class Test
{
public static void main(String[] args)
{
Week[] w=Week.values();
for(Week w1:w)
{
System.out.println(w1+" ------------- "+w1.ordinal());
}
}
}
When compare to old language enum, java enum is more powerful because in addition
to constants
we can declare variables, methods and constructors.
ex:
----
enum Cloths
{
SILK,COTTON,KHADI;
Cloths()
{
System.out.println("constructor");
}
}
class Test
{
public static void main(String[] args)
{
Cloths c=Cloths.SILK;
}
}
ex:
---
enum Cloths
{
SILK,COTTON,KHADI;
Singleton Class
================
Singleton is one of the design pattern which allows us to create only one object
for a class.
Using a class name if we call any method and that method returns same class object
is called singleton class.
ex:
Calendar c = Calendar.getInstance();
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
private Singleton()
{
}
return singleton;
}
}
class Test
{
public static void main(String[] args)
{
Singleton s1=Singleton.getInstance();
System.out.println(s1.hashCode());
Singleton s2=Singleton.getInstance();
System.out.println(s2.hashCode());
Singleton s3=Singleton.getInstance();
System.out.println(s3.hashCode());
}
}
Wrapper classes
===============
The main objective of wrapper classes are.
ex:
Primitive type wrapper class
----------------- -------------
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
constructor
------------
There are two ways to create object for wrapper classes. One will take
corresponding primitive as an argument and another will take corresponding String
as an argument.
ex:
wrapper class constructor
----------------- -------------
Byte byte or String
Short short or String
Integer int or String
Long long or String
Float float or String
Double double or String
Boolean boolean or String
Character char
ex:
---
class Test
{
public static void main(String[] args)
{
Integer i1=new Integer(10);
System.out.println(i1); //10
ex:
---
class Test
{
public static void main(String[] args)
{
Boolean b1=new Boolean(true);
System.out.println(b1);
ex:
---
class Test
{
public static void main(String[] args)
{
Character c=new Character('a');
System.out.println(c);//a
}
}
Utility methods
===============
1) parseXxx()
--------------
It is used to convert string to primitive type.
ex:
class Test
{
public static void main(String[] args)
{
String str="10";
int i= Integer.parseInt(str);
System.out.println(i); // 10
long l= Long.parseLong(str);
System.out.println(l); // 10
float f= Float.parseFloat(str);
System.out.println(f); // 10.0
double d= Double.parseDouble(str);
System.out.println(d); // 10.0
}
}
2) toString()
----------------
It is used to convert wrapper object to string.
ex:
--
class Test
{
public static void main(String[] args)
{
Integer i1=new Integer(10);
System.out.println(str); // 10
}
}
3) xxxValue()
---------------
It is used to convert wrapper object to primitive type.
ex:
class Test
{
public static void main(String[] args)
{
Integer i1=new Integer(10);
byte b = i1.byteValue();
System.out.println(b);
short s = i1.shortValue();
System.out.println(s);
}
}
input:
1010
0101
output:
1111
ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int c = a + b;
Assignment
==========
Q) Write a java program to display second highest element from given array?
input:
6 9 2 4 1 2 7
output:
7