Java Cheat Sheet to prepare for exams and tests PDF

Title Java Cheat Sheet to prepare for exams and tests
Author Tabitha Hoovere
Course Topics in Computer Architecture
Institution University of Virginia
Pages 5
File Size 72.6 KB
File Type PDF
Total Downloads 66
Total Views 129

Summary

Quick reference to go over concepts in Java programming....


Description

University of Virginia, Department of Computer Science

Java Cheat Sheet

Dr. Mark R. Floryan September 3, 2018

1 Java: General Things to Remember 1. All code must be inside of a class definition (except import and package statements). 2. Every line of code must end with a semi-colon. This excludes lines that formulate "blocks", like if(), while(), or class declarations. 3. The name of the class in a file must match the name of the file. For example, "public class LinkedList" must be in a file called "LinkedList.java" 4. Classes can contain a method "public static void main(String[] args)" as an entry point to the whole program. 5. Whitespace does NOT matter in java. The compiler will completely ignore all whitespace.

2 Primitive Data Types The primitive variable types are:

1

1

3

int x = 5; // integers d ou bl e d = 3.4; // d ec im al val ue s char c = ’h ’; // c ha rac te rs . U se sin gl e q uotes . bo ol ean b = false ; // true or fa lse

5

7

9

/* Othe r much less c om mo nl y use d */ byte b = 24 b; sho rt s = -8s ; long l = 2 0 0 0 0 0 0 ; flo at = 4. 56 7; Some examples of using these data types include:

2

int x; x ++; x - -;

// au tom a tic al l y set to 0 by d e fa ul t // inc rem en t in te ger by 1 // d ec rem en t in te ger by 1

4

int z = 14; 6

8

10

12

int total = (x + z) * x; int re m a in d e r = x % z;

// e xp re s s io n s // rem a in d e r afte r x / z

/* Boo le an op e ra to rs */ bo ol ean b1 = false ; bo ol ean b2 = true ; bo ol ean res ult ;

14

16

res ul t = b1 && b2 ; res ul t = b1 || b2 ; re su lt = ! b1 ;

// l ogic al AND // l ogic al OR // l ogic al ne ga tion

3 Input Output Input from the keyboard can be done like this: 1

3

5

7

Sc an ner in = new Scan ne r ( System . in ); int x = in . nex tIn t () ; d ou bl e y = in . ne xtD ou bl e (); flo at f = in . ne xt Fl oa t (); bo ol ean b = in . ne xtB oo lea n (); long l = in . n ex tLo ng (); St ri ng s = in . ne xt ();

// make // re ad // re ad // re ad // re ad // re ad // re ad

a sca nn er objec t int f rom keyboa rd do uble fro m ke ybo ard f loa t f rom keybo ard bo ol f rom keybo a rd lo ng f rom keybo a rd string from keybo ard

2

Input from a file can be done like this: 1

3

Bu f fe re d R e ad e r in = new B uf fe re d R ea d e r ( new Fil e R e a d e r ( " in p u tf il e . txt" )); St ri ng text = in . re ad L ine (); // re ad s the next line in . clo se (); Output to the console is done like this:

2

4

// p rin ts text co n ca ten a te d with x Sys te m . out . p rin t ( " The a nsw er is " + x ); // p rin ts and mo ves cu rso r to next line Sys te m . out . pri nt ln ( " so me th in g el se " ); Output to a file can be done like this:

1

3

5

PrintW rite r ou tFil e = new P rin tW rite r ( new Fil e W rite r ( " o u tp u tf il e . txt " ))); ou tFile . print ( " Hel lo " ); ou tFile . p rintln ( " wor ld " ); ou tFile . cl ose ();

4 Strings Strings are reference types in Java (so they are NOT primitives). 1

3

Strin g s1 = " Hell o " ; St ri ng s2 ; Strin g s3 = new String ( " Hi " );

// e xam pl e strin g // "" em pty strin g by d e fa ul t // Also make s a s tring

Common operators on strings include: 1

3

5

7

9

Strin g re su lt ; res ul t = s1 + s2 ; s1 . le ng th () ; s1 . ch ar At (2); s1 . su bs tr ing (1 ,3); s1 . eq u als ( s2 ); s1 . t oU pp erC as e (); s1 . t oL ow erC as e ();

// " hi " + " th er e " = " hi t he re " // returns len gth of strin g // a c ce ss es the cha r at po s itio n 2 ( in d e xe d f rom 0) // part of s tring from ind e x 1 to 3 ( e xcl us ive ) // c om pa re strings us ing th is stru cture // returns the strin g as all up pe rc as e // returns the strin g as all lo we rc as e

3

5 Converting Between Data Types In Java, we often need to convert between different types of variables. Here are some common conversions: 1

3

5

7

9

11

13

15

17

/* int ( or any o th er pr im it iv e ) to st rin g */ int x = 5; Strin g s = "" + x; // "" + va riable c on ca te na tes as a strin g /* String ve rs io n of n umber to int or d ou ble */ int i = In te ge r . pa rs eInt ( " 1 23 " ); // c on ve rts strin g "1 23" into intege d ou bl e d = Dou bl e . p arse Dou ble ( " 3.14 " ) // co nverts string " 3.1 4" in to d o ubl /* d ivid ing integers d oes integer d ivis io n */ int x1 = 3; int x2 = 5; d ou bl e re su lt = x1 / x2 ; // 3 /5= ( int )0.6 = 0 res ul t = ( d ouble ) x1 / ( d ou ble ) x2 ; // 0.6 /* d ou ble to int */ d ou bl e x = 3.46 7; int y = ( int )x; // y is 3 , d ec ima l t run ca te d

6 Arrays Three primary ways to instantiate arrays: 1

d ou bl e [] d Array = new do uble [5]; int [] od d Num be rs = {1 ,3 ,5 ,7 ,9};

// kn ow the size , but not co nten ts // kn ow the con te nts al re ad y

3

5

int x; /* Stuf f he re */ Strin g [] s Array = new S tr ing [ x ];

// use varia bl e to intial ize arra y

int [][] = new int [5 ][4];

// two - d im ens ion al array

7

Some common things we do with arrays include: 2

x [3] = 5; x. len gth ;

4

/* How to loop th ro ugh an a rra y */

// Acce ss array at p os itio n 3, set to 5. // get the n umber of el em en ts in array

4

6

for ( int i =0; i < x . le ng th ; i ++){ Sys te m . out . pri nt ln ( x[ i ]); }

7 Java Math Library Java contains several functions inside the Math class that are useful. Among them: 1

3

5

7

9

11

13

Math . abs ( x ); Math . max (a , b ); Math . min (a , b ); Math . sin ( the ta ); Math . cos ( the ta ); Math . tan ( the ta ); Math . toR ad ia ns ( d eg ); Math . toD eg re es ( rad ); Math . exp ( x ); Math . log ( x ); Math . pow (a , b ); Math . sq rt ( a ); Math . E; Math . PI ;

// a bs ol ute va lue of x // l arger of a and b // s ma ll er of a and b // sin trig fu n ctio n // cos trig fu n ctio n // ta ngen t trig fun c tion // co nvert d eg to rad ia ns // co nvert rad to d egre es // ra is es e ^x // na tu ra l lo ga rithm // raise a to pow er of b // sq u are root of a // va lue of c on stan t e // pi

5...


Similar Free PDFs