CSE 14 Lecture Notes - Lecture 3: Standard Streams, Bracket

67 views6 pages
21 May 2018
School
Course
Professor
CMPS12A Lecture 3 Intro. to Java Programming Part 2
Operators
+, -, *, /, %, …
6 + 5 = 11
hello + old -> helloold //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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
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;
od = Hello;
word2 = , Wold!;
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 “tig tpe. Fo “tigs, e use   to idiate that it is of that type. word1
euals the stig Hello ad od euals the stig , Wold!. 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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

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.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents