This document is a cheat sheet for Java fundamentals. It covers Java data types, statements, operators, and methods for String, ArrayList, HashMap, formatting numbers and dates. It also includes a basic Hello World example.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views
Sschaub Java-Fundamentals
This document is a cheat sheet for Java fundamentals. It covers Java data types, statements, operators, and methods for String, ArrayList, HashMap, formatting numbers and dates. It also includes a basic Hello World example.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
Java Fundamentals Cheat Sheet
by sschaub via cheatography.com/1000/cs/464/
Java Data Types Java Statements (cont) Java String Methods
byte / short / int / -123, 10 switch ( expression ) { s .length() length of s
long case value: s .charAt(i) extract ith character float / double 235.13 statements s .substring(start, substring from start to break; char 'U' end) end-1 case value2: boolean true, false s .toUpperCase() returns copy of s in ALL statements String "Greetings from CAPS break; earth" default: s .toLowerCase() returns copy of s in statements lowercase Java Statements } s .indexOf( x ) index of first occurence If Statement Exception Handling of x if ( expression ) { try { s .replace(old , search and replace statements statements; new) } else if ( expression ) { } catch ( ExceptionType e1) { s .split(regex) splits string into tokens statements statements; } catch (Exception e2) { s .trim() trims surrounding } else { catch-all statements; whitespace statements } } finally { s .equals(s2 ) true if s equals s2 While Loop statements; s .compa‐ 0 if equal/+ if s > s2/- if while ( expression ) { } reTo(s2 ) s < s2 statements See http://docs.oracle.com/javase/6/d‐ } Java Data Conversions ocs/api/java/lang/String.html for more. Do-While Loop String to Number do { int i = Integer.parseInt(str); java.util.ArrayList Methods statements double d = Double.parseDouble(str); l.add(itm ) Add itm to list } while ( expression ); Any Type to String For Loop String s = String.valueOf(value); l.get(i) Return ith item for ( int i = 0; i < max; ++i) { Numeric Conversions l.size() Return number of items statements int i = (int) numeric expression; l.remove(i) Remove ith item } l.set(i, val) Put val at position i For Each Loop for ( var : collection ) { ArrayList<String> names = statements new ArrayList<String>(); } Switch Statement See http://docs.oracle.com/javase/6/d‐ ocs/api/java/util/ArrayList.html for more.
By sschaub Published 17th July, 2012. Sponsored by CrosswordCheats.com
cheatography.com/sschaub/ Last updated 12th May, 2016. Learn to solve cryptic crosswords! Page 1 of 2. http://crosswordcheats.com Java Fundamentals Cheat Sheet by sschaub via cheatography.com/1000/cs/464/
m.put(key,value) Inserts value with key ! x (not) x && y (and) x || y (or)
m.get(key) Retrieves value with key Java Text Formatting
m.conta‐ true if contains key printf style formatting
insKey( key) System.out.printf("Count is %d\n", count); s = String.format("Count is %d", count); HashMap<StÂrinÂg,String> names = MessageFormat style formatting new HashMap<StÂrinÂg, String>(); s = MessageFormat.format( "At {1,time}, {0} eggs hatched.", See http://docs.oracle.com/javase/6/d‐ 25, new Date()); ocs/api/java/util/HashMap.html for more. Individual Numbers and Dates s = NumberFormat.getCurrencyInstance() Java Hello World .format(x); import java.util.Date; s = new SimpleDateFormat(""h:mm a"") public class Hello { .format(new Date()); public static void main(String[] args) { s = new DecimalFormat("#,##0.00") System.out.println("Hello, world!"); .format(125.32); Date now = new Date(); See http://docs.oracle.com/javase/6/d‐ System.out.println("Time: " + now); ocs/api/java/text/package-frame.html for } MessageFormat and related classes }
* Save in Hello.java * Compile: javac Hello.java * Run: java Hello
[FREE PDF sample] (Ebook) Learn PHP 8: Using MySQL, JavaScript, CSS3, and HTML5 by Steve Prettyman ISBN 9781484262399, 9781484262405, 1484262395, 1484262409 ebooks