Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Variables & Operators

18 February 2019
Contents
• Java Data Types
• Identifiers, Variables
• Operators
Java Data Types
https://image.slidesharecdn.com/lect3-basicsuserinputdatatypeconstructor-150219175632-conversion-gate01/95/java-basics-user-
input-data-type-constructor-18-638.jpg?cb=1424368743
Identifiers
• Identifiers are names (given by programmers) for
specific:
– classes
– variables
– methods
• Identifiers may not be any of the Java reserved
keywords.
• Identifiers cannot include spaces.
Java Reserved Keywords
Variables
• A variable is a named location that stores a value.
• Values may be numbers, text, images, sounds,
and other types of data.
• To store a value, you first have to declare a
variable. Variables must be initialized (assigned
values for the first time) before they can be used.
• A literal is a value of certain type.
• Variable names should be descriptive, of any
length; must start with a letter, an underscore or
dollar sign (rare). Must not start with number.
• Avoid using use special characters (+ - & * @
etc.)
• Uppercase and lowercase characters are distinct.
Which one?
• dayOfMonth Legal
• 2Dgraph Illegal
• December2020 Legal
• Hashtag#KawKaw Illegal
• Quiz 1 Illegal
Constants
• public/private static final TYPE NAME = VALUE;

can be accessed by all class


Constants
• public/private static final TYPE NAME = VALUE;

value to be shared amongst all instances of an object


Constants
• public/private static final TYPE NAME = VALUE;

value cannot be changed after initialization


string literal
Precedence of arithmetic operators
- (unary negation) right to left
*/% left to right
Arithmetic Operators +- left to right

https://www.tutorial4us.com/java/operators-in-java
https://www.w3schools.in/java-tutorial/operators/unary/
Arithmetic Operators

• 1 / 2 = 0 Integer division will truncate any decimal remainder


• 8 + 12 * 2 – 4 = 28
• (4 + 17) % 2 – 1 = 0
http://www.atnyla.com/tutorial/int---data-type-in-java/0/26
http://www.atnyla.com/tutorial/float---data-type-in-java/0/28
Relational Operators
Logical Operators
Assignment Operators

-
Conditional (Ternary) Operator

Expression 1 Expression 2 Expression 3


(condition) (if condition (if condition
is TRUE) is FALSE)
a == b “Yes” “No”
Using Classes from the Java Libraries
java.lang.Math

https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
Vocabulary
Review Questions
• Calculate the GPA, given the letter grade and
credit hours.
• Given a mathematical / scientific formula,
convert to Java program. Test using extreme
and normal values.
Resources
• BlueJ http://bluej.org/
• Think Java (Allen Downey & Chris Mayfield)
– http://greenteapress.com/thinkjava6/thinkjava.pdf
• Online Java Compiler
– https://www.tutorialspoint.com/compile_java_online.php
– https://www.jdoodle.com/online-java-compiler
– https://www.compilejava.net
• Online Java Tutorial
– https://docs.oracle.com/javase/tutorial/java/
– https://www.sitesbay.com/java/index
– https://www.w3schools.in/java-tutorial/
– https://www.tutorialspoint.com/java/

You might also like