0% found this document useful (0 votes)
222 views

Derived Syntactical Constructs in Java: K. K. Wagh Polytechnic, Nashik-3

Wrapper classes in Java wrap primitive data types like int and double in objects. This allows primitive types to be used in object-oriented ways like adding them to collections. Wrapper classes provide methods to convert between primitive types and their corresponding wrapper objects. Java also supports enumerated data types using the enum keyword. Enums allow for a fixed set of constants like days of the week. Enums can have fields, constructors, methods and implement interfaces.

Uploaded by

Shobhit Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

Derived Syntactical Constructs in Java: K. K. Wagh Polytechnic, Nashik-3

Wrapper classes in Java wrap primitive data types like int and double in objects. This allows primitive types to be used in object-oriented ways like adding them to collections. Wrapper classes provide methods to convert between primitive types and their corresponding wrapper objects. Java also supports enumerated data types using the enum keyword. Enums allow for a fixed set of constants like days of the week. Enums can have fields, constructors, methods and implement interfaces.

Uploaded by

Shobhit Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

K. K.

Wagh Polytechnic, Nashik-3

Chapter 2

Derived Syntactical Constructs in


Java
Wrapper Class
Wrapper classes wrap primitive types (eg: int, double, etc) in
Objects which can be placed into Vectors, and many, many other
uses.
Wrapper Class Uses
1. Convert primitive numbers to equivalent objects using constructors.
Ex: int a=10;
Integer i1=new Integer(a);
2. Convert objects numbers to primitive numbers using typeValue()
method.
Ex: Integer i1=new Integer(10);
int a=i1.intValue();
3. Convert numbers to strings using toString() method.
Ex: int x=20;String str =Integer.toString(x);
4. Convert string to numeric object using valueOf() method.
Ex: Integer obj ; String str =”12”;
obj= Integer.valueOf(str);
Wrapper Class Uses Cont.
5. Convert numeric strings to primitive numbers using parsing
methods using parseType()
Ex: String str = “12”;
int x =Integer.parseInt(str)
Visibility Control
• Many times it becomes necessary in some situations to restrict the
access to certain variables and methods from outside the class.
• There are following access specifiers in Java:
--public
--protected
--friendly or default
--private
public
protected
default or friendly
private
Access Protection Table
Java Enumerated Data Type:
• Enum in java is a data type that contains fixed set of constants.
•  It can be used for days of the week (SUNDAY, MONDAY,
TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and
SATURDAY) , directions (NORTH, SOUTH, EAST and
WEST) etc.
• The java enum constants are static and final implicitly. It is
available from JDK 1.5.
•  Java Enums can be thought of as classes that have fixed set of
constants.
Java Enumerated Data Type cont.:
 Points to remember for Java Enum:
• enum improves type safety
• enum can be easily used in switch
• enum can be traversed
• enum can have fields, constructors and methods
• enum may implement many interfaces but cannot extend any
class because it internally extends Enum class
Java Enumerated Data Type cont.:
class EnumExample1
{  
public enum Season { WINTER, SPRING, SUMMER, FALL }  
   public static void main(String[] args) 
{  
for (Season s : Season.values())  
System.out.println(s);  
   }
}  
 Output: WINTER
SPRING
SUMMER
FALL

You might also like