Quantcast

P^i

Your Online Tech Magazine

Tue05212013

Last update04:23:48 AM

Back You are here: Home More Programming and Web Programming Literals, Assignments, and Variables - Part 2

Literals, Assignments, and Variables - Part 2



Literals, Assignments, and Variables contd...

Boolean Literals
    Boolean literals are the source code representation for boolean values. A boolean value can only be defined as true or false. Although in C (and some other languages) it is common to use numbers to represent true or false, this will not work in Java. Again, repeat after me, "Java is not C++."
    
boolean t = true; // Legal

boolean f = 0; // Compiler error!

Be on the lookout for questions that use numbers where booleans are required. You might see an if test that uses a number, as in the following:

int x = 1; if (x) { } // Compiler error!

Character Literals
A char literal is represented by a single character in single quotes.

char a = 'a';

char b = '@';

You can also type in the Unicode value of the character, using the Unicode notation of prefixing the value with \u as follows:

char letterN = '\u004E'; // The letter 'N'

Remember, characters are just 16-bit unsigned integers under the hood. That means you can assign a number literal, assuming it will fit into the unsigned 16-bit range (65535 or less). For example, the following are all legal:

char a = 0x892; // hexadecimal literal

char b = 982; // int literal

char c = (char)70000; // The cast is required; 70000 is // out of char range

char d = (char) -98; // Ridiculous, but legal

And the following are not legal and produce compiler errors:

char e = -29; // Possible loss of precision; needs a cast

char f = 70000 // Possible loss of precision; needs a cast

You can also use an escape code if you want to represent a character that can't be typed in as a literal, including the characters for linefeed, newline, horizontal tab, backspace, and single quotes.

char c = '\"'; // A double quote

char d = '\n'; // A newline

Literal Values for Strings
    A string literal is a source code representation of a value of a String object. For example, the following is an example of two ways to represent a string literal:
    
String s = "Bill Joy";
System.out.println("Bill" + " Joy");

Although strings are not primitives, they're included in this section because they can be represented as literals—in other words, typed directly into code. The only other nonprimitive type that has a literal representation is an array, which we'll look
at later in the chapter.

Thread t = ??? // what literal value could possibly go here?

Assignment Operators
Assigning a value to a variable seems straightforward enough; you simply assign the stuff on the right side of the = to the variable on the left. Well, sure, but don't expect to be tested on something like this:

x = 6;

No, you won't be tested on the no-brainer (technical term) assignments. You will, however, be tested on the trickier assignments involving complex expressions and casting. We'll look at both primitive and reference variable assignments. But before we begin, let's back up and peek inside a variable. What is a variable? How are the variable and its value related?

    Variables are just bit holders, with a designated type. You can have an int holder, a double holder, a Button holder, and even a String[] holder. Within that holder is a bunch of bits representing the value. For primitives, the bits represent a numeric value (although we don't know what that bit pattern looks like for boolean, luckily, we don't care). A byte with a value of 6, for example, means that the bit pattern in the variable (the byte holder) is 00000110, representing the 8 bits. So the value of a primitive variable is clear, but what's inside an object holder? If you say,

Button b = new Button();

    what's inside the Button holder b? Is it the Button object? No! A variable referring to an object is just that—a reference variable. A reference variable bit holder contains bits representing a way to get to the object. We don't know what the format
is. The way in which object references are stored is virtual-machine specific (it's a pointer to something, we just don't know what that something really is). All we can say for sure is that the variable's value is not the object, but rather a value
representing a specific object on the heap. Or null. If the reference variable has not been assigned a value, or has been explicitly assigned a value of null, the variable holds bits representing—you guessed it—null. You can read

Button b = null;

as "The Button variable b is not referring to any object."

So now that we know a variable is just a little box o' bits, we can get on with the work of changing those bits. We'll look first at assigning values to primitives, and finish with assignments to reference variables.








blog comments powered by Disqus