Java Complex Flashcards
What does the word static do.
Call a static method using a class name Math.min(88,86);
Call a non-static method using a reference variable name Song t2 = new Song(); t2.play(); Static doesn't need a NEW object.
Syntax for wrapping a primitive
wrapping a value int i = 288; Integer iWrap = new Integer(i);
Syntax for unwrapping a syntax
unwrapping a value
int unWrapped = iWrap.intValue();
List of wrappers
Boolean Character Byte Short Integer Long Float Double
Format of a fomat modifier(%)
%[argument number][flags][width][.precision]type
ex.format(“%,6.1f”, 42.000)
Types of Format modifiers
%d decimal
%f floating point
%x hexadecimal
%c character
Types of date format modifiers.
The complete date and time %tc String.format(“%tc”, new Date()); Sun Nov 28 14:52:41 MST 2004
Just the time %tr
String.format(“%tr”, new Date());
03:01:47 PM
Day of the week, month and day %tA %tB %td
Date today = new Date();
String.format(“%tA, %tB %td”,today,today,today)
Sunday, November 28
WHat does a try/catch do and syntax.
Tells compiler that something may go wrong please prepare for it.
try { // do risky thing } catch(Exception ex) { // try to recover }
Syntax for creating a new socket.
Socket chatSocket = new Socket(“196.164.1.103”, 5000)
To read data from a socket.
1. Make a Socket connection to the server Socket chatSocket = new Socket(“127.0.0.1”, 5000);
2. Make an InputStreamReader chained to the Socket’s low-level (connection) input stream InputStreamReader stream = new InputStreamReader(chatSocket.getInputStream());
3. Make a BufferedReader and read! BufferedReader reader = new BufferedReader(stream); String message = reader.readLine();
To write data to a Socket
1. Make a Socket connection to the server Socket chatSocket = new Socket(“127.0.0.1”, 5000); 2.Make a PrintWriter chained to the Socket’s low-level (connection) output stream PrintWriter writer = new PrintWriter(chatSocket.getOutputStream()); 3.Write (print) something writer.println(“message to send”); writer.print(“another message”);