Copyright

Difference Between Integer & String in Java

Instructor: Sudha Aravindan

Sudha has a Doctor of Education degree in math education and is currently working as a Information Technology Specialist.

The Integer class in Java holds numbers, and the String class holds characters that are enclosed in double quotes. In this lesson, we will review the Integer and String classes in the Java programming language.

Integer Class in Java

The Integer class in Java is what is known as a wrapper class. Wrapper classes in Java are used to convert a data type to an Object. The data type 'int' in Java is a primitive or basic data type and contains only numeric data. The size of an int in Java is 32 bits. An example of declaring an int in Java is:


int x = 30;


This statement declares a primitive int variable x and initializes it with the value 30.

To convert the primitive data type int to the Integer object in Java, we would say:


Integer z = new Integer (x);


This statement creates a new Integer object and a reference to the object is assigned to the variable z.

Methods Supported by the Integer Class

The data field in an Integer object can be accessed only through the methods of the Integer class.

intValue( )

One method is the intValue( ) method. This captures or returns the numerical integer or int value that is referred to by the Integer object. This is an example of this method:


int y = z.IntValue();


Now y will contain the integer value 30.

But this is incorrect:


int y = z;


It will not work because z is an Integer object and y is a primitive int. So, the Integer object has to be converted to a primitive int using the method intValue()

toString( )

The following statement is an example of the toString( ) method:


String s = Integer.toString(y);


This converts the integer value y = 30 to the String value ''30'' since the String object is always enclosed in quotes in Java.

String Class in Java

The String class in Java is a sequence of characters. This can include numbers, alphabetical letters, and special characters. A String in Java is always enclosed in double quotations. This is an example of declaring a String in Java.


String hello = "Hellow World!"


Methods Supported by the String Class

The values stored in a String class in Java can be accessed using the methods of the String class.

charAt( )

This method returns the character stored at the specified index.


String a = "apple";
System.out.println(a.charAt(3));


This will print the value 'l' since the index of a string starts from 0. So, charAt(3) is the 4th character of the string.

indexOf( )

This method returns the index of the first occurrence of the specified character within the string.


String k = "Zebra"
System.out.println(indexOf("r"));


This will print the int value 3 since the index starts from 0.

length( )

The length( ) method returns the number of characters in the string.


String k = "yellow rose"
System.out.println(k.length());


This will print 11 since there are 11 characters in the string yellow rose, including the one space between yellow and rose, and counting characters starts at 1.

replace( )

The replace( ) method replaces a character with another character.


String k = "yellow rose"
System.out.println(k.replace("s", "p"));


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support