01.01 L

531
-1

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
531
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

01.01 L

  1. 1. First Programs, Variables, Integers, and the Assignment Operator Lecture 01.01
  2. 2. What Does a Java Program Look Like? <ul><li>public class MyFirstProgram </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(“Hello, world!&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Note: The program must be saved in a file named MyFirstProgram.java (case sensitive!) </li></ul>
  3. 3. print vs. println <ul><li>There are two methods that print to the screen: </li></ul><ul><ul><li>System.out.print(&quot;text here&quot;); </li></ul></ul><ul><ul><li>System.out.println(&quot;text here&quot;); </li></ul></ul><ul><li>print leaves the writing marker on the same line. </li></ul><ul><li>println advances the writing marker to the next line. </li></ul>
  4. 4. Examples of print & println <ul><li>The output of this code segment </li></ul><ul><li>System.out.print(&quot;This text is&quot;); </li></ul><ul><li>System.out.print(&quot;on the same line.&quot;); </li></ul><ul><li>is </li></ul><ul><li>This text ison the same line. </li></ul>
  5. 5. Examples of print & println <ul><li>The output of this code segment </li></ul><ul><li>System.out.println(&quot;This text is&quot;); </li></ul><ul><li>System.out.print(&quot;on another line.&quot;); </li></ul><ul><li>is </li></ul><ul><li>This text is </li></ul><ul><li>on another line. </li></ul>
  6. 6. What is the output? <ul><li>public class CrazyPrint </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;Can you&quot;); </li></ul><ul><li>System.out.print(&quot;guess&quot;); </li></ul><ul><li>System.out.println(); </li></ul><ul><li>System.out.print(&quot; this?&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  7. 7. Compilation Errors <ul><li>If the syntax is not correct Java syntax, a compilation error will be generated. </li></ul><ul><li>Example - System.out.prntln(&quot;hi&quot;); generates the following error: </li></ul><ul><ul><li>C:DesktopMyFirstProgram.java:5: cannot find symbol </li></ul></ul><ul><ul><li>symbol : method prntln(java.lang.String) </li></ul></ul><ul><ul><li>location: class java.io.PrintStream </li></ul></ul><ul><ul><li>System.out.prntln(&quot;hi&quot;); </li></ul></ul><ul><ul><li>^ </li></ul></ul><ul><ul><li>1 error </li></ul></ul>
  8. 8. Variables <ul><li>A variable is a named memory location. </li></ul><ul><li>This code segment prints 56 to the screen: </li></ul><ul><ul><li>int x; </li></ul></ul><ul><ul><li>x = 5; </li></ul></ul><ul><ul><li>System.out.print(x); </li></ul></ul><ul><ul><li>x = 6; </li></ul></ul><ul><ul><li>System.out.println(x); </li></ul></ul><ul><li>Note: </li></ul><ul><ul><li>int specifies the type of values the variable can hold (in this case, integers) </li></ul></ul><ul><ul><li>x is the name of the variable </li></ul></ul><ul><ul><li>A variable must* be declared ( int x ) before it is initialized ( x = 5 ) and initialized before it is used. </li></ul></ul>
  9. 9. The Assignment Operator <ul><li>The assignment operator assigns what is on the right side of the operator to the variable on the left. </li></ul><ul><li>Example code segment that prints 13 </li></ul><ul><ul><li>int x; //declares x </li></ul></ul><ul><ul><li>x = 5; //assigns 5 to x </li></ul></ul><ul><ul><li>x = 5 + 5; //assigns 10 to x, x is no longer 5 </li></ul></ul><ul><ul><li>x = x + 3; //assigns 13 to x, x is no longer 10 </li></ul></ul><ul><ul><li>System.out.print(x); //Prints 13 to the screen </li></ul></ul>
  10. 10. Legal Identifiers <ul><li>An identifier is used to name variables and other things. </li></ul><ul><li>A legal identifier is any number of letters (a-z,A-Z), digits (0-9), the dollar sign ($), or the underscore(_). However, the first digit can't be a digit. </li></ul><ul><li>A Java reserved word can't be an identifier. </li></ul>
  11. 11. Example Identifiers <ul><li>x </li></ul><ul><li>stockPrice </li></ul><ul><li>firstName </li></ul><ul><li>I_love_COMP_$ci </li></ul><ul><li>$_$_$_$ </li></ul><ul><li>Hi12345 </li></ul>
  12. 12. Incorrect Identifiers <ul><li>1stName (starts with a digit) </li></ul><ul><li>First name (two different identifiers) </li></ul><ul><li>I_Just_Love_* (* is not legal) </li></ul>
  13. 13. Identifiers with Style <ul><li>While there are many legal identifiers, in this class the proper style for variables will be to start with a lower case letter and capitalize each word after that. </li></ul><ul><li>testScore </li></ul><ul><li>finalAverage </li></ul><ul><li>player1 </li></ul><ul><li>numberOfTurns </li></ul>
  14. 14. Java Reserved Words <ul><li>The following words can not be used as identifiers: </li></ul><ul><li>abstract continue for new switch </li></ul><ul><li>assert*** default goto* package synchronized </li></ul><ul><li>boolean do if private this </li></ul><ul><li>break double implements protected throw </li></ul><ul><li>byte else import public throws </li></ul><ul><li>case enum**** instanceof return transient </li></ul><ul><li>catch extends int short try </li></ul><ul><li>char final interface static void </li></ul><ul><li>class finally long strictfp** volatile </li></ul><ul><li>const* float native super while </li></ul><ul><li>*  not used </li></ul><ul><li>**  added in 1.2 </li></ul><ul><li>*** added in 1.4 </li></ul><ul><li>**** added in 5.0 </li></ul>
  15. 15. More About Integers <ul><li>The Java virtual machine uses 32 bits (32 0's and 1's) to represent an integer. </li></ul><ul><li>1 bit is used as a sign bit (+/-) </li></ul><ul><li>31 bits are used for the number </li></ul><ul><ul><li>Min number is -2 31 = -2,147,483,648 </li></ul></ul><ul><ul><li>Max number is 2 31 -1 = 2,147,483,647 </li></ul></ul>
  16. 16. Integer Overflow <ul><li>System.out.println(2147483647); </li></ul><ul><li>prints the number correctly </li></ul><ul><li>System.out.println(2147483648); </li></ul><ul><li>causes a compilation error </li></ul><ul><li>System.out.println(2147483647+1); </li></ul><ul><li>prints -2147483648 </li></ul><ul><li>System.out.println(2147483647+2); </li></ul><ul><li>prints -2147483647 </li></ul><ul><li>System.out.println(214748364*100); </li></ul><ul><li>prints -80 </li></ul>
  17. 17. Adding Comments to Code <ul><li>Comments are notes that are added to a program that the compiler will ignore </li></ul><ul><li>There are two styles </li></ul><ul><ul><li>// comments out from // to end of the line </li></ul></ul><ul><ul><li>/* */ comments out everything from /* to */ </li></ul></ul><ul><li>Examples: </li></ul><ul><ul><li>int totalSales; //total sales for the fiscal quarter </li></ul></ul><ul><ul><li>/* print error msg */ System.out.println(&quot;***&quot;); </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×