Automatic Conversion between Primitive Types and Wrapper Class Types Converting

Automatic conversion between primitive types and

This preview shows page 38 - 48 out of 54 pages.

Automatic Conversion between Primitive Types and Wrapper Class Types Converting a primitive value to a wrapper object is called boxing . The reverse conversion is called unboxing . Java allows primitive types and wrapper classes to be converted automatically. The compiler will automatically box a primitive value that appears in a context requiring an object, and will unbox an object that appears in a context requiring a primitive value. This is called autoboxing and autounboxing . 11/08/2019 OBJECT ORIENTED PROGRAMMING 38
Image of page 38

Subscribe to view the full document.

Automatic Conversion between Primitive Types and Wrapper Class Types For instance, the following statement in (a) can be simplified as in (b) due to autoboxing. 11/08/2019 OBJECT ORIENTED PROGRAMMING 39
Image of page 39
Automatic Conversion between Primitive Types and Wrapper Class Types Consider the following example: 1) Integer[] intArray = { 1 , 2 , 3 }; 2) System.out.println(intArray[ 0 ] + intArray[ 1 ] + intArray[ 2 ]); In line 1, the primitive values 1 , 2 , and 3 are automatically boxed into objects new Integer(1) , new Integer(2) , and new Integer(3) . In line 2, the objects intArray[0] , intArray[1] , and intArray[2] are automatically unboxed into int values that are added together. 11/08/2019 OBJECT ORIENTED PROGRAMMING 40
Image of page 40

Subscribe to view the full document.

The String Class 11/08/2019 OBJECT ORIENTED PROGRAMMING 41
Image of page 41
The String Class A string is a sequence of characters. The char type represents only one character. To represent a string of characters, use the data type called String . For example, the following code declares message to be a string with the value "Welcome to Java" . String message = "Welcome to Java" ; String is a predefined class in the Java library 11/08/2019 OBJECT ORIENTED PROGRAMMING 42
Image of page 42

Subscribe to view the full document.

Getting String Length String message = "Welcome to Java" ; System.out.println( "The length of message is " + message.length() ); OR System.out.println( "The length of message is " + "Welcome to Java".length() ); Note that "" denotes an empty string and "".length() is 0 . 11/08/2019 OBJECT ORIENTED PROGRAMMING 43
Image of page 43
Getting Characters from a String 11/08/2019 OBJECT ORIENTED PROGRAMMING 44
Image of page 44

Subscribe to view the full document.

Getting String Length String message = "Welcome to Java" ; System.out.println( "The Character at 0 th index is " + message.charAt(0) ); System.out.println( "The Character at 14 th index is " + message.charAt(14) ); 11/08/2019 OBJECT ORIENTED PROGRAMMING 45
Image of page 45
Concatenating Strings You can use the plus ( + ) operator to concatenate strings The following code combines the strings message , " and " , and "HTML" into one string: String message = "Welcome to Java" ; String myString = message + " and " + "HTML" ; + operator can also concatenate a number or character with a string. If one of the operands is a non-string, the non-string value is converted into a string and concatenated with the other string. Here are some examples: String s = "Chapter" + 2 ; // s becomes Chapter2 String s1 = "Supplement" + “B” ; // s1 becomes SupplementB 11/08/2019 OBJECT ORIENTED PROGRAMMING 46
Image of page 46

Subscribe to view the full document.

Reading a Character from the Console To read a character from the console, use the nextLine() method to read a string and then invoke the charAt(0) method on the string to return a character. For
Image of page 47
Image of page 48
  • Fall '14
  • Dr Haroon

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes