CSE 14 Lecture Notes - Lecture 3: Standard Streams, Bracket
CMPS12A Lecture 3 – Intro. to Java Programming Part 2
Operators
+, -, *, /, %, …
6 + 5 = 11
hello + old -> helloold //This + is a concatenation for strings
Variables
A variable is an identifier used to name an area of memory.
Variable Name
Memory location
Variables are created using declaration statements in this way:
type var1, var2, var3;
Example:
int i;
int j, k;
String word1, word2;
Example:
int i = 6, j = 7, k;
i j k
6 7
String word = Hello;
word1
Hello
find more resources at oneclass.com
find more resources at oneclass.com
Just to note from last lecture, an integer contains 32 bits, so where 6 is stored in variable i and 7
is stored in variable j, each variable now has a value with 32 bits.
….110 <- this equals 6 in binary with 32 bits
… <- this equals 7 in binary with 32 bits
Here is an example of an actual program that compiles:
Example:
class HelloWorld {
public static void main(String [] args) {
String word1, word2, sentence;
od = Hello;
word2 = , Wold!;
sentence = word1.concat(word2); //same as sentence = word1 + word2;
System.out.println(sentence);
}
}
As stated before, we write class and name followed by open curly bracket. After that, we go
into this main function of the program that will be the heart, basically where everything will be
printed out to output. We always need to write public static void main(String [] args) to make
this main function; trust that it is how it works. Everything to why this is the case will become
clear in future lectures but as of now, lets move into the variables. We initiate a variables
word1, word2, and sentence of String type inside the main function, and then we make those
variables equal a “tig tpe. Fo “tigs, e use to idiate that it is of that type. word1
euals the stig Hello ad od euals the stig , Wold!. As you can see in sentence, we
use variable word1 and initiate a method concatenate with .concat(word2) which adds both
strings together. (More of methods and classes later in the lectures). We finally print out what
is in sentence with System.out.println(sentence). So as you can see, whatever you want to print
out to output, you would write this, replacing sentence with whatever you need to print out of
course.
To also point out, one can initiate a value to a variable after declaring it, so like this:
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
Cmps12a lecture 3 intro. to java programming part 2. 6 + 5 = 11 (cid:862)hello(cid:863) + (cid:862)(cid:449)o(cid:396)ld(cid:863) -> (cid:862)hello(cid:449)o(cid:396)ld(cid:863) A variable is an identifier used to name an area of memory. Variables are created using declaration statements in this way: type var1, var2, var3; Example: int i = 6, j = 7, k; i. Here is an example of an actual program that compiles: Example: class helloworld { public static void main(string [] args) { String word1, word2, sentence; (cid:449)o(cid:396)d(cid:1005) = (cid:862)hello(cid:863); word2 = (cid:862), wo(cid:396)ld! (cid:863); sentence = word1. concat(word2); As stated before, we write class and name followed by open curly bracket. After that, we go into this main function of the program that will be the heart, basically where everything will be printed out to output. We always need to write public static void main(string [] args) to make this main function; trust that it is how it works.