Logout

D.3.4

Describe the uses of the primitive data types and the reference class string.

Teaching Note:

In examination questions the primitive types will be limited to int, long, double, char and Boolean.

MYP Mathematics: forms of numbers.

Sample Question:

sdfsdfsf

JSR Notes:

Firstly trying to figure out how to interpret the assessment statement and teaching note: Let's assume this is saying "describe the use of" int, long, double, char, boolean, and String. And that it's not so concerned about the fact that String is a class, but can also be used as a literal type, like primitives.

So it's simply, "describe the use of int", with an answer that is really basic, but that someone walking down the street would not be able to answer.

So: int: int is used to represent medium length integer values.

Here are the others:

long: long is a data type used to represent very large integer values.

double: double is a data type which is used to represent real number values, i.e. those with decimals.

char: char is a data type used to represent individual characters, in fact, those characters represented in the ASCII table, such as 'a', 'A', '2', and '!'.

boolean: boolean is a data type used to represent either true or false, and that's it.

string: string is a data type used to represent a string of characters; i.e. words. (Again, regardless of whether String is being treated as a literal, or a reference type.)

### Strings as Literal Primitives vs Objects

The rest of this page of notes goes a bit beyond what is required, particularly for Standard Level, but going through it will help consolidate a lot of things, and make some of the Higher Level concepts make a lot more sense later on.

String can be treated as a primitive, but can also be treated as a "reference object", when we call the constructor of String, using the new operator.

String as literal

String x = "hello world";

String as object

String y = new String("hello world");

You'll recall from the definitions of D.3.1 that primitives are literally their value in memory, and that their variable can be seen as a short-cut for that literal value. And String can work that way.

But String, when treated as a "reference object", is indeed a reference, i.e. an address reference indicating where in memory the String object - it's char array, and its String attributes and methods, such as length - reside.

A Bit More on the Peculiarity of Strings

Strings are strings of characters. And so a Java string is an array of char primitives. This actually makes string technically more a data structure, rather than a primitive. In fact, if you wanted, you could declare an array of chars every time you wanted to work with words. But this would be pretty silly. So the makers of Java decided that since strings are so commonly used that they would give them the ability to be treated as literal primitives.

But the makers of Java also saw strings as something which could be nicely be controlled by a specific String class, which could have pre-made attributes and methods that are commonly used when working with words. Attributes such as length could be kept track of and directly used, and methods such as substring for accessing a subset of the character array could also be pre-implemented and available to be easily used.

And so the Java String is a very unique thing within the world of Java. It is actually a structure made up of primitive chars, yet it can itself be declared and used as a primitive, and it can also be declared and used as an object of a class, namely the String class.

### String Equality vs Identity

When working with Strings it is imporant to keep in mind the difference between equality and identiy, and the usage of the .equals( ) method, vs. the == operator. The two Strings have "equality", when what they are representing are the same word/s. But their "identity" refers to specifically where they are in memory. So two Strings could have equal equality, but not have equal identiy. See the diagram below, but here's a simple example:

String a = "sun";
String b = new String("sun");
String c = new String("sun);

a, b, and c all have equality, but each has a different identity.

So, a.equals(b), and a.equals(c), and b.equals(c) are all trure.

But a == b, and a == c, and b == c are all false.

Generally, we as programmers will usually be interested in the equality sense of equals, so as a rule of thumb, always use .equals( ) to test for equality of Strings in Java.

**The below diagram continues the trend of most of this page of notes being a bit beyond what is required for IB CS, but it's good to have an idea about for a variety of reasons.**

EXTRA - NOT AT ALL NEEDED (THOUGH INTERESTING)

One otherĀ minorĀ thing simply to note at this time is that Strings are "immutable", meaning that (similar to the way that an array cannot grow) once they are made, they cannont be changed. It may seem as if you can change a String with lines like:

String x = "hello world";
x = "hello world extended a bit";

But what is actually happening here is that the first array of chars h e l l o w o r l d gets "orphaned", and the variable x will create a new literal string of chars another place. Sooner or later Java's garbage collection will realize that the first array of chars is not being pointed to by anything, and so will "mop it up", but it is a bit inefficient in terms of memory usage. This is particularly true in any loop which seems to work with the same string loop after loop, when actually it is instantiating a new String every time through loop. This isn't that big a deal, and the IB curriculum donesn't worry about it, but for what it's worth, there is one way around this, and that is to use the Java method StringBuffer instead. See Morelli page 318 for more information on this class