
Quick Contact
Java Tutorial
- What is Java?
- History of Java
- Hello Java Program
- Features of Java
- Basic Syntax
- Java Setup
- Data Types in Java
- Java Variables
- Operators in Java
- JVM
- Java If-else Statement
- Switch Case Statement
- Java Loops
- Do-While Loop
- Java While Loop
- Continue Statement
- Break Statement in Java
- Constructors in Java
- Oops Concept in Java
- Features of OOPs
- Inheritance
- Exeception handeling
- Aggregation (HAS-A relationship) in Java
- Method Overriding in Java
- Method Overloading
- Java Static Keyword
- Java This Keyword
- Java Final Keyword
- Polymorphism
- Static Binding and Dynamic Binding
- Abstract class in Java
- Access Modifiers in Java
- Difference between abstract class and interface
- Interface in Java
- Garbage Collection in Java
- Java Package
- Encapsulation
- Serialization and Deserialization in Java
- Java Inner Classes
- Java Applets
- Multithreading in Java
- Thread Priorities in Java
- Thread Creation
- Inter Thread Communication
- Wrapper Classes in Java
- Java Input Output
- Java AWT Introduction
- Java Layout Manager
- Java Layout Policy
- Java AWT Events
- Collection Framework
- Collection Framework List Interface
- Swing in Java
- Swing Utility Classes
- Swing Layout Managers
- Java JDBC
- Hibernate Framework Overview – Architecture and Basics
Springboot
- Spring Environment Setup
- Spring Boot CRUD REST API Project using IntelliJ IDEA | Postman | MySQL
- Dockerizing Spring Boot Application | Spring Boot Docker Tutorial
- spring-boot-restapidocumentation with swagger
- Spring Boot HttpClient Overview
- Apache HttpClient POST HTTP Request Example
- Apache HttpClient PUT HTTP Request Example
- Apache HttpClient DELETE HTTP Request Example
- Apache HttpClient HTML Form POST Request Example
- Spring Boot JSP Exampl
- Deploying Spring Boot WAR file with JSP to Tomcat
- Spring Boot Annotations
- Spring Core Annotations
- Spring MVC Annotations with Examples
- Spring Scheduling Annotations
- Spring - Java-based Container Configuration
- Spring Java Based Configuration Example
Hibernate
- Hibernate 5 hello world
- Hibernate- One to One Unidirectional Mapping Annotation Example
- Hibernate - Batch Processing
- Hibernate - Interceptors
- Hibernate 5 - Create, Read, Update and Delete (CRUD) Operations Example
- Hibernate Transaction Management
- Hibernate One to Many Unidirectional Mapping Example
- Hibernate One to Many Bidirectional Mapping Example
- Hibernate Many to Many Annotation Mapping Example
- Hibernate Primary KeyJoin Column
- Hibernate First Level Cache with Example
- Hibernate XML Configuration Example with Maven + Eclipse + MySQL Database
- Hibernate Java Configuration Example
- JPA 2 with Hibernate 5 Bootstrapping Example
- JPA and Hibernate Cascade Types
- Hibernate/JPA - Primary Key Generation
- Hibernate 5 - Enum Type Mapping Example
- Hibernate Component Mapping
- Hibernate Object States – Transient,Persistent and Detached
- Hibernate 5 - Save an Entity Example
- Hibernate 5 - Persist an Entity Example
- Hibernate 5 - saveOrUpdate() Method Example
- Hibernate 5 - get(), load() and byId() Method Examples
- Hibernate 5 - merge() Example
- Hibernate 5 - Delete or Remove an Entity Example
- Hibernate 5 - load() Method Example
- Hibernate Session Interface Methods
- Hibernate Session.clear() Method Example
- Introduction Of Java strutes to Architecture
- Struts 2 - Architecture
- Struts 2 - Configuration Files
- Struts 2 - Actions
- Struts 2 - Interceptors
- Struts 2 - Results & Result Types
- Struts 2 - Value Stack/OGNL
- Struts 2 - File Uploading
- Struts 2 - Database Access
- Struts 2 - Validations Framework
JAVA FX
- JavaFX Tutorial
- Introduction to JavaFX Pane
- JavaFX Popup
- JavaFX group
- JavaFX Controller
- JavaFX Gradient Color
- JavaFXAnchorPane
- JavaFXTabPane
- JavaFX Scene
- JavaFX Stage
- JavaFXWebView
- JavaFX Timeline
- JavaFX Timer
- JavaFX Image
- JavaFX Background
- JavaFX dialog
- JavaFX Font
- JavaFXTextArea
- JavaFXObservableList
- JavaFX GUI
- JavaFX FXML
- JavaFXEventHandler
- JavaFXGradle
- JavafxScrollpane
- JavaFXAPI
Wrapper Classes in Java
The primitive data types in java are not objects. If we want to use these data types as objects, then we will have to use wrapper classes for each of these primitive data types provided in java.lang package.
There are many built-in classes, which cannot handle primitive data types as they deal only with objects. One such class is Vector, which is used to store a collection of objects. We cannot use the Vector class to directly store the collection of elements of a primitive data typed. But we can do so by storing the objects of wrapper classes, which correspond to the primitive data types.
The Java has wrapper class corresponding to each of the primitive data type as shown in the following table:
Primitive Data Types | Wrapper Class |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
Number class:
The abstract class Number defines super-class that is implemented by the classes that wrap the numeric type byte, short, int, long, float, and double. Number class has abstract methods that return the value of the object in each of the different number formats. These methods are:
byte byteValue()
Returns the value of the specified number as a byte.
short shortValue()
Returns the value of the specified number as a short.
abstract int intValue()
Returns the value of the specified number as an int.
abstract long longValue()
Returns the value of the specified number as a long.
abstract float floatValue()
Returns the value of the specified number as a float.
abstract double doubleValue()
Returns the value of the specified number as a double.
Note-
The values returned by byteValue(), shortValue(), intValue(),longValue(), floatValue() and doubleValue() methods may involve rounding or truncation.
Example
The following example demonstrates how rounding and truncation takes place when invoking methods of class Number.
class Wrapper { public static void main(String args[ ]) { Integer iObj = new Integer(128); System.out.println(iObj.byteValue()); //truncation Long lObj = new Long(123456789123456789L); System.out.println(lObj); System.out.println(lObj.doubleValue()); Float fObj = new Float(3.99f); System.out.println(fObj.intValue()); truncation } }
Output
-128
123456789123456789
1.23456789123456784E17
3
Converting Primitive Numbers to Objects using Constructors of Wrappers Classes and Converting Numeric Objects back to Primitive Numbers:-
Example
The following example demonstrates how primitives can be wrapped in objects and how they can be converted back to primitives.
class Convert { public static void main(String args[]) { System.out.println(“Converting primitive numbers to objects “ + “using constructors”); byte b = 105; Byte bObj = new Byte(b); System.out.println(bObj); //toString() short s = 2015; Short sObj = new Short(s) System.out.println(sObj); int i=32717; Integer iObj = new Integer(i); System.out.println(iObj); long l = 234543335565675L Long lObj = new Long(l); System.out.println(lObj); float f = 3.1415f; Float fObj = new Float(f); System.out.println(fObj); double d = 3.1415; Double dObj = new Double(d); System.out.println(dObj); System.out.println(“Converting numeric objects to primitive numbers”); byte b1 = bObj.byteValue(); short s1 = sObj.shortValue(); int i1 = iObj.intValue(); long l1 = lObj.longValue(); float f1 = fObj.floatValue(); double d1 = dObj.doubleValue(); System.out.println(b1); System.out.println(s1); System.out.println(i1); System.out.println(l1); System.out.println(f1); System.out.println(d1); } }
Output:
Converting primitive numbers to objects using constructor
105
2015
32717
234543335565675
3.1415
3.1415
Converting object to primitive numbers
105
2015
32717
234543335565675
3.1415
3.1415
Converting Primitive Numbers to Strings using toString() static method of the corresponding Wrapper Class
Example
class ConvertPrimitiveToString { public static void main(String args[ ]) { System.out.println(“Converting primitive numbers to String “ + “using toString() static method of corresponding wrapper class:”); byte b = 105; String str=Byte.toString(b); System.out.println(str); short s=303; str = Short.toString(s); System.out.println(str); int i=100; str = Integer.toString(i); System.out.println(str); long l=454444444444l; str = Long.toString(l); System.out.println(str); float f=3.444f; str=Float.toString(f); System.out.println(str); double d=3.44444; str=Double.toString(d); System.out.println(str); } }
Output
Converting primitive numbers to String using toString() static method of
corresponding wrapper class:
105
303
100
454444444444
3.444
3.44444
Converting Numeric Objects to Strings using toString() method of the corresponding Wrapper Class:-
Example
class ObjectToStringDemo { public static void main(String args[ ]) { System.out.println(“Converting object numbers to Strings using” + “toString() method of corresponding wrapper class:”); byte b=103; Byte bObj = new Byte(b); String str=bObj.toString(); System.out.println(str); short s=203; Short sObj=new Short(s); str = sObj.toString(); System.out.println(str); Integer iObj = new Integer(32000); str = iObj.toString(); System.out.println(str); str = new Long(4544444444444l).toString(); System.out.println(str); str = new Float(3.1444f).toString(); System.out.println(str); str = new Double(4.1444).toString(); System.out.println(str); } }
Output:
Converting object numbers to Strings using toString() method of corresponding
wrapper class:
103
203
32000
4544444444444
3.1444
4.1444
Converting String Objects(Numeric Strings) to Numberic Objects using the static valueOf() method of the corresponding Wrapper Class
Example
class StringToNumericObjectDemo { public static void main(String args[]) { String str=”30”; String str2=”30.333”; Byte bObj=Byte.valueOf(str); System.out.println(bObj); //Byte bObj1 = new Byte(str2); //NumberFormatException Short sObj = Short.valueOf(str); System.out.println(sObj); Integer iObj=Integer.valueOf(str); System.out.println(iObj); Long lObj=Long.valueOf(“344324232432”); System.out.println(lObj); Float fObj=Float.valueOf(“3.333”); System.out.println(fObj); Double dObj=Double.valueOf(str2); System.out.println(dObj); } }
Output:
30
30
30
3.44324232432
3.33
30.333
Note:-
All of the valueOf() methods throw “NumberFormatException” if the string does not contain a parsable number.
Converting string Objects (Numeric Strings) to Numeric Objects using Constructor of the corresponding Wrapper Class
Example
class StringToNumericObjectDemo1 { public static void main(String args[]) { String str=new String(“30”); //String str=”30”; String str2=new String(“30.333”); Byte bObj = new Byte(str); System.out.println(bObj); //Byte bObj=new Byte(str2); //NumberFormatException Short sObj=new Short(str); System.out.println(sObj); Integer iObj=new Integer(str); System.out.println(iObj); Long lObj=new Long(str); System.out.println(lObj); Float fObj=new Float(str); System.out.println(fObj); Double dObj=new Double(str); System.out.println(dObj); } }
Output:
30
30
30
30
30.333
30.333
Note:-
The Above constructor throw “NumberFormatException” if the string does not contain a parsable number.
Converting String Objects (Numeric Strings) to Primitive Numbers using parsing methods of the corresponding Wrapper Class
Example
class StringToPrimitiveDemo { public static void main(String args[]) { String str = new String(“30”); //String str=”30”; String str2= new String(“30.333”); byte b = Byte.parseByte(str); System.out.println(b); //byte b1=Byte.parseByte(str2); //NumberFormatException short s = Short.parseShort(str); System.out.println(s); int i = Integer.parseInt(str); System.out.println(i); long l = Long.parseLong(str); System.out.println(l); float f = Float.parseFloat(str2); System.out.println(f); double d = Double.parseDouble(str2); System.out.println(d); } }
Output:
30
30
30
30
30.333
30.333
Note:-
parseXXXXX() methods throw “NumberFormatException” if the string does not contain a parsable number.
Apply now for Advanced Java Training Course