Download presentation
Presentation is loading. Please wait.
Published byChester Carroll Modified over 8 years ago
1
Lecture 3.1: Operator Precedence and Eclipse Tutorial Michael Hsu CSULA
2
Clarifications for Lecture 3 a % b If a > b, then it returns the remainder of the division If a < b, then it returns a If a == b, then it returns 0 Testing even and odd If x is even, then x % 2 == 0 evaluates to true If x is odd, then x % 2 == 1, or you can use x % 2 != 0
3
More Clarifications Recall the fun example: int i =10; int k = i+++i; System.out.println(k); Why is k == 21?
4
Expressions, Literals, and Statements Literals: Source code representation of a fixed value Examples: 1, 2, 0, -200, true, false, ‘A’ Expression: Made up of variables, operators, that evaluates to a single value Java subexpressions are evaluated left to right Examples: 2 * 3 * 4 2 % 3 i+++I K =10 Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html
5
Statements “sentences” with semi colons at the end Assignment Any Use of ++ or – You will lean different types later
6
Operator Precedence In Java Some key words: Precedence Order: When two operators share an operand the operator with the higher precedence goes first Example: 1 + 2 * 3 is the same as 1 + (2 * 3) Associativity: When an expression has two operators with the same precedence, the expression is evaluated according to its associativity Example: x = y = z= 17 is evaluated as x = ( y= (z = 17)), because = operator has right to left associativity Source: http://introcs.cs.princeton.edu/java/11precedence/
7
Precedence and Associativity Table http://introcs.cs.princeton.edu/java/11precedence/ http://introcs.cs.princeton.edu/java/11precedence/
8
So how does i+++i work? int i =10; int k = i+++i; System.out.println(k); i+++i is equivalent to (i++)+i, since ++ has precedence over + We evaluate from left to right Since it is post increment, i++ evaluates to 10, (10) + i After i++ is evaluated, we increment i so i becomes 11 10+11 = 21
9
Using variables in String literals We did System.out.println() with String literals inside: System.out.println(“2 divided by 3 in java is zero”); To use variables in String literals, use the + operator: int num1 = 2; int num2 = 3; System.out.println(num1+ " divided by "+num2+" in Java is " + 2/3);
10
Eclipse Tutorial Follow the eclipse setup guide in the resources section on the course website Key terms: Workspace Your working directory it will contain multiple projects When you want to move your eclipse files from file explorer, move the entire workspace folder. Projects This is where you will put your java files You can have one workspace for all your courses, and have different projects for each course, or have separate workspaces for each course and have separate projects for each assignments I recommend having a different workspace for each of your courses with separate projects for each assignment of the course Makes your life easier
11
Useful Eclipse Tips Show Line Numbers: Window -> Preferences -> expand General -> expand Editors -> click on Text Editors -> check show line numbers Ctrl+ SPACE Autocomplete, auto fix Ctrl + SHIFT + f Auto format and indentation Ctrl + d Delete entire line Right click on a variable then click refactor You can rename your variables and Eclipse will update all the occurrences for you
12
DO NOT delete files in your eclipse workspace from file explorer Eclipse uses special files to track how your project directory is set up, your interface layout, etc. Do everything from within eclipse so you don’t screw up your projects I’ll try to put up an step by step tutorial on the course webpage
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.