0% found this document useful (0 votes)
281 views

Java Programming Cheat Sheet

This document provides an overview of key Java concepts: 1. Java runs in a Java Virtual Machine (JVM) which translates Java code into bytecode compatible with operating systems. The JVM can be obtained from open source providers. 2. Classes are grouped into packages and imports allow accessing external libraries. Variables must be strongly typed and can be instance, static, or local. 3. Methods can be public, private, protected, or default. The try/catch block handles exceptions and finally ensures code runs regardless of other results.

Uploaded by

bnechita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
281 views

Java Programming Cheat Sheet

This document provides an overview of key Java concepts: 1. Java runs in a Java Virtual Machine (JVM) which translates Java code into bytecode compatible with operating systems. The JVM can be obtained from open source providers. 2. Classes are grouped into packages and imports allow accessing external libraries. Variables must be strongly typed and can be instance, static, or local. 3. Methods can be public, private, protected, or default. The try/catch block handles exceptions and finally ensures code runs regardless of other results.

Uploaded by

bnechita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Programming Cheat Sheet PAGE 1 of 2 BY SETH KENLON

Java runs in a Java Virtual Machine (JVM), a layer that translates Java code into
bytecode compatible with your operating system. Get an open source JVM from
openjdk.java.net or developers.redhat.com/products/openjdk/download

Java Packages
Related classes are grouped into a package. Declare a When creating libraries, you can create packages within a
package name at the top of your code. package to provide access to each unique class.

package com.opensource.hello; package com.opensourc.greeting;


/* @author your-name */ /* @author your-name */
Java Imports Java Variables
When importing libraries needed for your Java code, use Java is strongly typed, meaning all variable types must be
the import key word. Imports work based on your declared when created. Local variables may be created
environment's Java path, and open source libraries can be inside a method, instance variables may be created inside
bundled with your application (license permitting.) a class, and static variables may be shared across
instances.
package com.opensource.hello; //code...
/* @author your-name */ public class Greeting {
static int num = 42;
import java.lang.System; public static String namer() {
import javax.swing.*; String name = "Java";
int number =1;
double bignum = 3.1415926535897932384;
//code...
float smallnum = 3.141592;
char character = 'a';
boolean toggle = true;
return name; }
}

Java Classes Java Methods


A class file must contain one public class. Other classes Java methods may be public (accessed by any other
may exist elsewhere and may not be public. By class), private (known only within a class), protected
convention, class names start with a capital letter. (unavailable to an unrelated class), or default. Provide the
type of returned data, such as void, int, float, etc.

package com.opensource.hello; //code...


/* @author your-name */ public static String namer() {
String name = "Java";
import java.lang.System; return name; }
}
public class Greeting {
public static String namer() { public static void main(String[] args) {
//code... String player = greeting.namer();
} System.out.printf("%s,%s", named, num);
} }
}

opensource.com Twitter @opensourceway | facebook.com/opensourceway | CC BY-SA 4.0


opensource.com: Java Programming Cheat Sheet PAGE 1 of 2 BY SETH KENLON

Try and Catch


To catch errors in Java, start with try, fall back on catch, and end with finally. Should the try clause fail, then
catch is invoked, and in the end, there's finally to perform some action regardless of the results.

try {
cmd = parser.parse(opt, args);

if(cmd.hasOption("help")) {
HelpFormatter helper = new HelpFormatter();
helper.printHelp("Hello <options>", opt);
System.exit(0);
}
else {
if(cmd.hasOption("shell") || cmd.hasOption("s")) {
String target = cmd.getOptionValue("tgt");
} // else
} // if
} catch (ParseException err) {
System.out.println(err);
System.exit(1);
} //catch
finally {
new Hello().helloWorld(opt);
} //finally
} //try
Arguments Run a .java file
Passing arguments into a Java application is done with Java files, usually ending in .java, can be run from your
the args keyword, preceded by the type of acceptable IDE or in a terminal using the java command. If an
arguments (such as String, int, and so on). application is complex, however, running a single file may
not be useful. Add arguments as appropriate.

//code... $ java ./Example.java


public static void main (String[] args) { $ java ./diceroller.java 20
System.out.printf("You rolled a "); You rolled a 17
DiceRoller App =
new DiceRoller( Run a JAR file
Integer.parseInt(args[0]) ); Usually, Java applications are distributed as Java
Archives (JAR) files, ending in .jar. To run a JAR file,
App.Roller(); double­click its icon or launch it from a terminal.
}
$ java -jar /path/to/Example.jar
}

opensource.com Twitter @opensourceway | facebook.com/opensourceway | CC BY-SA 4.0

You might also like