May 26, 2018
Hot Topics:

Primitives and Object Wrappers

  • June 6, 2006
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Another interesting feature with the codes is the relationship between lowercase and uppercase letters. Take a look at Listing 3.

public class Primitives {

   public static void main(String[] args) {

      char c1 = 'a';
      char c2 = 'A';

      char c3 = 'b';
      char c4 = 'B';

      System.out.println("a = " + (short)c1);
      System.out.println("A = " + (short)c2);

      System.out.println("b = " + (short)c3);
      System.out.println("B = " + (short)c4);

      System.out.println("a diff = " + ((short)c1 - (short)c2));
      System.out.println("b diff = " + ((short)c3 - (short)c4));



Listing 3

In this example, there are two sets of chars defined. One set is a lowercase 'a' and an uppercase 'A'. The second is a lowercase 'b' and an uppercase 'B'. Not surprisingly, the lowercase and uppercase chars have different numeric codes. Of even greater interest is that the difference between the lowercase and uppercase values of both the 'a' set and the 'b' set are the same (the value 32). The way the code tables are set up, this ration is set for all of the lowercase and uppercase letters.

When the code is executed, you get the results in Figure 2.

Figure 2

Although there are only a limited number of characters on the keyboard, there is a much richer set available to the programmer. For example, if you look at the code table you will find that the value for PI symbol is 227. You then can print this out programmatically, with the code in Listing 4.

public class Primitives {

   public static void main(String[] args) {

      char c = 'a';

      short num = 227;    // The code for PI

      System.out.println("c = " + (short)c);
      System.out.println("num = " + (char)num);


Listing 4

When you run the code, as seen in Figure 3, the actual π symbol is displayed. Thus, you do have a wide variety of symbols that can be used.

Figure 3

Characters also can be escape sequences. The original octal escape sequences look like the familiar newline escape sequence that harkens back to the days of the C Programming Language:

char c2 = 'n';    \ ASCII escape sequence for newline

These types of escape sequences are supported, as well as the Unicode escape sequences that are in the following format:

char c1 = 'u0022';    \ Unicode escape sequence for double quote

Listing 5 shows how these escape sequences are used within the code.

public class Primitives {

   public static void main(String[] args) {

      char c = 'a';

      short num = 227;    // The code for PI

      System.out.println("c = " + (short)c);
      System.out.println("num = " + (char)num);


Listing 5

The output from the code is shown in Figure 4. Notice that a double quote is displayed and that there is an extra line before the last line of output.

Figure 4


The Java primitives that you just covered are not objects. By definition, objects have attributes and behaviors, which the primitive data types don't have. Other than these primitives, everything else in Java is an object. Whereas a character represents a single Unicode value, you can treat a string as an object that contains a collection of characters.

In fact, to obtain the functionality of a string, the C Programming Language used a character array to contain the individual units of the string.

char string[100];

You would literally manipulate the string as a collection of characters.

C++ provided some extensions, specifically a string class (using the The C++ Standard Template Library (STL)) that effectively wrapped the functionality of the character array into a nice package.

#include <string>
using std::string;

string s = "abc def abc";

Java has a class called String that was part of the original Java specification. As I continue the discussion on wrappers, you can think of a String class as a wrapper. It wraps the functionality of the character array that I mentioned earlier in a class. You then can create objects from this String class.

String s = "abc";

This String class provides the attributes and various methods that provide certain string manipulation functionality. The String class effectively wraps most of the functionality that you would need to process a string. For example, Listing 6 shows how the String method toUpperCase() works to manipulate the String object created.

public class Primitives {

   public static void main(String[] args) {

      String s1 = "abc";

      String s2 = s1.toUpperCase();

      System.out.println("s1 = " + s1);
      System.out.println("s2 = " + s2);


Listing 6

The output from the code is shown in Figure 5. In this example, there are actually two strings created, s1 and s2. The following code does not really change the original string; it uses s1 to create a new string using the toUpperCase()method.

String s2 = s1.toUpperCase();

Figure 5

Page 2 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date