Sponsored Link •
|
// Reference only Account acct; // Reference and object LogFile log = new LogFile(); // Two ways to create strings String s1 = new String("Hello, World!"); String s2 = "Hello, World!";
byte
,
short
, int
, long
, float
,
double
, boolean
, char
Byte
,
Short
, Integer
, Long
, Float
,
Double
, Boolean
, Character
int i = 2001; Integer i1 = new Integer(2001); Integer i2 = i1;
1 { 2 int i = 10; 3 // Only i in scope here 4 { 5 int j = 20; 6 // Both i and j in scope 7 // Note: can't redefine i here 8 } 9 // i still in scope, j out of scope 10 }
1 { 2 Integer i = new Integer(10); 3 { 4 Character c = new Character('q'); 5 } 6 // c out of scope 7 // Character object unreferenced 8 } 9 // i out of scope 10 // Integer object unreferenced
class Account { // class definition goes here }
Account acct = new Account();
class Account { // balance: in American pennies private long balance; // the methods... }
1 // In file classesandobjects/ex1/Account.java 2 3 public class Account { 4 5 // balance: in American pennies 6 private long balance; 7 8 // amount: in American pennies 9 public boolean okToWithdraw(long amount) { 10 return (amount <= balance); 11 } 12 13 // amount: in American pennies 14 // return value: in American pennies 15 public long withdraw(long amount) 16 throws InsufficientFundsException { 17 18 if (amount > balance) { 19 throw new InsufficientFundsException( 20 amount - balance); 21 } 22 23 balance -= amount; 24 return amount; 25 } 26 27 // amount: in American pennies 28 public void deposit(long amount) { 29 30 balance += amount; 31 } 32 }
Account acct = new Account(); acct.deposit(15000L); long cash = acct.withdraw(4000L);
public int lastIndexOf(String str, int fromIndex) { //... }
String
,
ArrayIndexOutOfBoundsException
replace()
, equalsIgnoreCase()
MAX_VALUE
, DECIMAL_DIGIT_NUMBER
String
java.lang.String
com.artima.jvmsim.JVMSimulator
java.lang
, java.io
, java.awt
,
java.util
, java.applet
java.util.HashMap h = new java.util.HashMap();
import java.util.HashMap; //... HashMap h = new HashMap();
import java.util.*; //... HashMap h = new HashMap();
java.lang
package imported automatically
static
keyword: "class variables" and "class methods"
class CoffeeCup { private static int cupCount; //... }
class CoffeeCup { private static int cupCount; public static int getCupCount() { return cupCount; } // ... } int count = CoffeeCup.getCupCount();
1 // In file classesandobjects/ex2/EchoArgs.java 2 3 public class EchoArgs { 4 5 public static void main(String[] args) { 6 7 int argCount = args.length; 8 for (int i = 0; i < argCount; ++i) { 9 10 if (i != 0) { 11 System.out.print(' '); 12 } 13 System.out.print(args[i]); 14 } 15 System.out.println(); 16 } 17 }
EchoArgs.java
:
1 // In file classesandobjects/ex2/EchoArgs.java 2 3 public class EchoArgs { 4 5 public static void main(String[] args) { 6 7 int argCount = args.length; 8 for (int i = 0; i < argCount; ++i) { 9 10 if (i != 0) { 11 System.out.print(' '); 12 } 13 System.out.print(args[i]); 14 } 15 System.out.println(); 16 } 17 }
Hello
that prints out a greeting, such as
the traditional, "Hello, world."
Feel free to use the traditional
greeting or come up with your own creative greeting.
EchoArgs.java
so that it changes all lower case
letters to upper case before echoing them.
For example, for the command line:
java UpperArgs Four sCore and seven years agothis program would print:
FOUR SCORE AND SEVEN YEARS AGO
Create a Java application named Counter
that prints out the numbers between 1 and 100, inclusive. (i.e.: 1, 2, 3, ..., 99, 100)
EchoArgs.java
so that it prints out the
arguments in reverse order, including the letters of the individual
arguments. For example, for the command line:
java ReverseArgs one two threethis program would print:
eerht owt eno
Sponsored Links
|