Java supports eight different primitive data types.
Java provides four ways to deal with numbers for different applications.
Real Numbers with decimals: The `float` and the `double` data types.
Real whole numbers: The int data type
Any ASCII value that can be represented in the machine is called a character (i.e., a char) . The char data type holds a single character and it must be enclosed inside the single quotes ' ' and represents a numeric value. For example:
> char c = 'a'
> c
'a'
> (int)c
97
The value 97 is obtained by typecasting to an integer the character c. Type casting means to force Java to transform a data type into another data type by specifying the data type inside the parenthesis, as shown (int). You can find the decimal value for the character 'a' and all the characters on your keyboard in the following table.
Java supports an additional data type called `String`. The String is a collection of characters that represent words or phrases.
A String is an Object that contains "functionalities" that allow you to do certain operations. These functionalities are called methods.
Methods are functionalities that belong to objects, such as String, which we will discuss more in detail on Ch. 4. Some of the popular methods can be seen in Table 3. Let us demonstrate these functionalities by testing them into the interactions window:
The length() method retrieves the total number of characters in the String. Let us define a String called s1, that will hold the String "hello", after calling the method, we obtain 5, since the String s1 has 5 characters, i.e., 'h', 'e', 'l', 'l', and 'o'.
> String s1 = "hello"
> s1
"hello"
> s1.length()
5
Java is a zero-based numbering language, meaning that we start counting from 0 and not from 1. Let us access each character from the String `hello` starting from index 0
> s1.charAt(0)
'h'
> s1.charAt(1)
'e'
> s1.charAt(2)
'l'
> s1.charAt(3)
'l'
> s1.charAt(4)
'o'
Although the String `hello` has a length of 5, a typical error into access the index representing the length of the String, in this case, is 5. This mistake will lead us to an exception (discussed on detail on Ch. Ch. 8) as follows:
> s1.charAt(5)
java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.String.charAt(Unknown Source)
Let us now redefine the variable s1, by providing a different value for the String.
> s1 = "hElLo gUyS hOw aRe yOu"
"hElLo gUyS hOw aRe yOu"
Notice that the variable s1 has a combination of the upper and lower case. The String counts with methods that change the case for the string. For example:
> s1.toUpperCase()
"HELLO GUYS HOW ARE YOU"
changes the String into upper case, where the toLowerCase() changes to a lower case.
> s1.toLowerCase()
"hello guys how are you"
It is important to recognize that if we call the variable s1, it will continue with the original state since we never update the variable.
> s1.toLowerCase()
> s1
"hElLo gUyS hOw aRe yOu"
Remember that the s1 is a variable, and the purpose of a variable is that its value eventually change. If that is the objective, then we need to assign (=) the new value of s1 to s1 as follows:
> s1 = s1.toLowerCase()
> s1
"hello guys how are you"
In addition to the functions (methods) listed above for the String, there is one additional called the substring that is quite useful for your programming projects.
The substring is a method from the object String that provides a partial subpart of the string depending on the two methods you use.
There are two methods for substring:
substring(pos): the substring from position 0 to position ‘pos’. E.g.,
If String s = “hello”, s.substring(3) will give you back the String: “lo”
substring(pos1, pos2): the substring from `pos1` to `pos2`. E.g.,
If String s = “hello”, s.substring(1,3) will give you back the String: “el”
In our previous example, we can demonstrate the two substring methods as follows:
> s1.substring(11)
"Lo gUyS hOw aRe yOu"
> s1.substring(6,11)
"gUyS h"
A variable is a placeholder for a value that can eventually change. You probably remember using variables in your math classes, such as x and y, and solving for each of them. A variable in programming refers to a piece of memory in the computer, where we store a value that we can use in the future.
Variables are used to store information in your programs that you will probably use later. To start, you need to specify the data type of a variable and provide a value that corresponds to that particular data type. For example, suppose that you would like to store the value 5 into a variable called x. the way to do it in Java is:
int x = 5;
it reads as follows: "store in memory a whole number (int) called x, that will “get” the value 5, end the command (;)”
Let us interact with Java to demonstrate the different variable manipulations by declaring two integer variables, x and y, with the values of 5 and 3 respectively.
> int x = 5
> int y = 3
We can take advantage that Java stores the values of `x` and `y` into memory, so we can use variables to perform operations such as:
> x + y
8
> x * y
15
> x % y
2
Furthermore, we can store the results of operations into new variables
> int z = x + y
> z
8
The variables are stored in memory based on their data types. Each data type has a capacity and properties that are used in different forms through programming. In the next section, we discuss this in more detail.
Because variables are placeholders, we must consider that the computer memory will keep track of the value and that we can change those values eventually. For example, considering the last interaction, the value of `z` is 8 because of the result of adding the variable `x` and `y`, whose values are `5` and `3` respectively. After that, we can still use the value of `z` including to the same `z`, like this
> int z = x + y
> z
8
> z = z - 2
> z
6
The statement `z = z - 2` it reads as follows: the variable z will be changed by using the old value of z (that is 8) and then subtract 2 (which will be 8 - 2 = 6) when the value of `z` is called, it will return the value of `6`.
AP CS A
[Scope, Comments, and Programming Style]
MOD-2.C Describe the functionality and use of program code through comments.
MOD-2.C.1 Comments are ignored by the compiler and are not executed when the program is run.
MOD-2.C.2 Three types of comments in Java include /* */, which generates a block of comments, //, which generates a comment on one line, and /** */, which are Javadoc comments and are used to create API documentation.
CS1
[Scope, Comments, and Programming Style]
130.421.c.2.d Write programs with proper programming style to enhance the readability and functionality of the code by using meaningful descriptive identifiers, internal comments, white space, spacing, indentation, and a standardized program style;
130.421.c.6.s Demonstrate an understanding of the concept of scope;
Design, write, test, and debug a program that effectively uses the different structured data types provided in the language like strings, arrays/lists, dictionaries, sets
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)