0% found this document useful (0 votes)
4 views20 pages

How To Think Like A Computer Scientist 2

The document provides an overview of programming concepts in Java, focusing on variables, types, and printing. It explains the importance of variable declaration, assignment, and the use of keywords and operators. Additionally, it highlights the rules of operation precedence and the limitations of mathematical operations on strings.

Uploaded by

rsalami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

How To Think Like A Computer Scientist 2

The document provides an overview of programming concepts in Java, focusing on variables, types, and printing. It explains the importance of variable declaration, assignment, and the use of keywords and operators. Additionally, it highlights the rules of operation precedence and the limitations of mathematical operations on strings.

Uploaded by

rsalami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming

with java
Variables and Types
More printing
More
printing
More printing
Strings - sequence of characters that
include letters, numbers, punctuation
marks and other

Println - print line, adds a newline


More printing
Strings - sequence of characters that
include letters, numbers, punctuation
marks and other

Println - print line, adds a newline

Spaces & line breaks - no impact but


good practice; make programs easier to
read
variables
Variables
Variable - named location that stores a value

To store a value, create a variable

[Type][Variable name] => String bob

Declaration - declares the variable to be of type bob

Type - every variable must have a type

Int vs int; string vs String (compiler objection)


Variables 2
Variable names - indicate what the variable does

String firstName;

String lastName;

int hour, minute;

Declares multiple variables, same type

Quiz: hour & minute are of what type?


assignme
nt
assignment
Variables created, let's store values in them with:

Assignment statement

So far:

Declaring a variable, creates named storage location

Make assignment to variable, gives variable a value


Variables 2
Some strings look like integers but are not
Printing
variables
Printing variables
Print the value of a variable
Keywords
keywords
Keywords - names reserved by Java

E.g. public, class, void, int

Development environment warning

Visit here for complete list of keywords


operators
operators
Addition is +

Subtraction is -

Multiplication is *

Division is /

The result?

Integer division(rounds down) Solution? Multiply


by 100 & floats

Operands
Operators: order of
operations
Precedence

multiplication and division before addition and subtraction

Same precedence

Evaluated left to right

Use parentheses to override rules of precedence


Operators for strings
Mathematical operations cannot be performed on Strings

Except concatenation

=>

=>
???
Composition
All of these are fine

NOT okay =>

You might also like