1 / 6

Wrapper Classes

Wrapper Classes. More than just a song and dance. What. They are objects that store primitive variable values: Integer – stores an int Double – stores a double Character – stores a char Boolean – stores a boolean. Why?.

rmalone
Download Presentation

Wrapper Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Wrapper Classes More than just a song and dance.

  2. What • They are objects that store primitive variable values: • Integer – stores an int • Double – stores a double • Character – stores a char • Boolean – stores a boolean

  3. Why? • Sometimes you want a primitive value stored as an object. This allows you to: • Store primitives in complex data structures such as ArrayLists • They are pass by reference instead of pass by copy • They provide some additional helper methods

  4. Helpful Methods • parseInt(String s) – takes in a String and converts it into the corresponding Integer • toHexString(inti) – converts an integer to a hexadecimal String • toBinaryString(inti) – converts an integer to a binary String

  5. Implementation //Declaring variables Integer x = 5; Double y = 6.7; Character z = 'z'; Boolean flag = false; //Using parseInt String numInText = "29274"; Integer a = Integer.parseInt(numInText); //Performing operation Integer b = x + a; System.out.println(b);

  6. Use in ArrayLists ArrayList <Integer> numbers = new ArrayList<Integer>(); numbers.add(3); numbers.add(5); numbers.add(7); System.out.println(numbers);

More Related