G 123456789 to 123456792108 long oat long double

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: alizaPon.java:4: variable vacaPonDays might not have been iniPalized if(vacaPonDays > 100) ^ 1 error CPSC 324 ‐‐ Spring 2010 32 16 1/14/10 Variable declaration vs. initialization •  This is a variable declaration int vacaPonDays; •  This is a variable initialization (also an assignment) vacaPonDays = 20; •  These can be combined in Java int vacaPonDays = 20; •  Declarations can be anywhere in your code double salary = 65000.00; System.out.println(salary); // print the value, add newline int vacaPonDays = 12; CPSC 324 ‐‐ Spring 2010 33 Constant variables •  Java uses the keyword “final” to denote constants –  A final variable cannot be changed after initialization final double CM_PER_INCH = 2.5; // do some stuff CM_PER_INCH = 2.54; // compile error!!! FinalTest.java:4: cannot assign a value to final variable CM_PER_INCH CM_PER_INCH = 2.54; ^ 1 error CPSC 324 ‐‐ Spring 2010 34 17 1/14/10 Class constants (fields) •  Many classes have constant fields that can be accessed within other classes and methods •  For example: public class Integer { public staPc final int MAX_VALUE = 2147483647; } •  Within our programs, we can access this constant value: public class MyClass { public staPc void main(String args) { System.out.print(“Guess a number between 1 and ” + Integer.MAX_VALUE); } } CPSC 324 ‐‐ Spring 2010 35 Converting between numeric types •  Lossless value conversions: –  byte → short –  short → int –  int → long –  int → double •  Allowed conversions that can lose precision: –  int → float (e.g., 123456789 to 1.23456792*108) –  long → float –  long → double CPSC 324 ‐‐ Spring 2010 36 18 1/14/10 Explicit casts •  You have to explicitly force a (potentially) lossy conversion –  For example double pi = 3.14159 int y = pi; // compile error! –  Instead, you have to do this double pi = 3.1.4159; One way to think about this is int y = (int) pi; that the compiler automatically casts the allowed conversions ... But not the disallowed ones CPSC 324 ‐‐ Spring 2010 37 Operators •  Java supports the normal arithmetic operators + (plus) – (minus) * (multiply) / (divide) % (modulus) •  And the various standard shortcuts –  x += 4; –  x++; –  ++x; // x = x + 4; // x; x = x + 1 // x = x + 1; x; CPSC 324 ‐‐ Spring 2010 38 19 1/14/10 Operators •  Java also supports the standard boolean operators && (logical and) || (logical or) == (value equal) != (value not equal) <, >, <=, >= –  When chaining these together (a && b || c), you should use parenthesis to make evaluation order clear to the “reader” •  Plus the bitwise ops –  & (and), | (or), ^ (xor), ~ (not), >>, << (bit shift) CPSC 324 ‐‐ Spring2010 39 Control Flow •  Standard conditionals: if-then...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 324 at Gonzaga.

Ask a homework question - tutors are online