Learn Java in Minutes
Learn Java in Minutes
Read
more here.
// Single-line comments start with //
/*
Multi-line comments look like this.
*/
/**
JavaDoc comments look like this. Used to describe the Class or various
attributes of a Class.
*/
// Import ArrayList class inside of the java.util package
import java.util.ArrayList;
// Import all classes inside of java.security package
import java.security.*;
// Each .java file contains one outer-level public class, with the same name as
// the file.
public class LearnJava {
// In order to run a java program, it must have a main method as an entry point.
public static void main (String[] args) {
// Use System.out.println() to print lines.
System.out.println("Hello World!");
System.out.println(
"Integer: " + 10 +
" Double: " + 3.14 +
" Boolean: " + true);
// To print without a newline, use System.out.print().
System.out.print("Hello ");
System.out.print("World");
// Use System.out.printf() for easy formatted printing.
System.out.printf("pi = %.5f", Math.PI); // => pi = 3.14159
///////////////////////////////////////
// Variables
///////////////////////////////////////
/*
* Variable Declaration
*/
// Declare a variable using <type> <name>
int fooInt;
// Declare multiple variables of the same type <type> <name1>, <name2>, <name3>
int fooInt1, fooInt2, fooInt3;
/*
* Variable Initialization
*/
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
// Strings
String fooString = "My String Is Here!";
// \n is an escaped character that starts a new line
String barString = "Printing on a new line?\nNo Problem!";
// \t is an escaped character that adds a tab character
String bazString = "Do you want to add a tab?\tNo Problem!";
System.out.println(fooString);
System.out.println(barString);
System.out.println(bazString);
// Arrays
// The array size must be decided upon instantiation
// The following formats work for declaring an array
// <datatype>[] <var name> = new <datatype>[<array size>];
// <datatype> <var name>[] = new <datatype>[<array size>];
int[] intArray = new int[10];
String[] stringArray = new String[1];
boolean boolArray[] = new boolean[100];
// Another way to declare & initialize an array
int[] y = {9000, 1000, 1337};
String names[] = {"Bob", "John", "Fred", "Juan Pedro"};
3
///////////////////////////////////////
// Operators
///////////////////////////////////////
System.out.println("\n->Operators");
int i1 = 1, i2 = 2; // Shorthand for multiple declarations
// Arithmetic is straightforward
System.out.println("1+2 = " + (i1
System.out.println("2-1 = " + (i2
System.out.println("2*1 = " + (i2
System.out.println("1/2 = " + (i1
System.out.println("1/2 = " + (i1
+
*
/
/
i2)); // => 3
i1)); // => 1
i1)); // => 2
i2)); // => 0 (int/int returns an int)
(double)i2)); // => 0.5
// Modulo
System.out.println("11%3 = "+(11 % 3)); // => 2
// Comparison operators
System.out.println("3 == 2? " + (3 == 2)); // => false
System.out.println("3 != 2? " + (3 != 2)); // => true
System.out.println("3 > 2? " + (3 > 2)); // => true
System.out.println("3 < 2? " + (3 < 2)); // => false
System.out.println("2 <= 2? " + (2 <= 2)); // => true
System.out.println("2 >= 2? " + (2 >= 2)); // => true
// Boolean operators
System.out.println("3 > 2 && 2 > 3? " + ((3 > 2) && (2 > 3))); // => false
System.out.println("3 > 2 || 2 > 3? " + ((3 > 2) || (2 > 3))); // => true
4
////////////////////////////////////////
// Converting Data Types And Typecasting
////////////////////////////////////////
// Converting data
// Convert String To Integer
Integer.parseInt("123");//returns an integer version of "123"
// Convert Integer To String
Integer.toString(123);//returns a string version of 123
//
//
//
//
//
//
//
//
Typecasting
You can also cast Java objects, there's a lot of details and deals
with some more intermediate concepts. Feel free to check it out here:
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
///////////////////////////////////////
// Classes And Functions
///////////////////////////////////////
7
// }
class Bicycle {
// Bicycle's Fields/Variables
public int cadence; // Public: Can be accessed from anywhere
private int speed; // Private: Only accessible from within the class
protected int gear; // Protected: Accessible from the class and subclasses
String name; // default: Only accessible from within this package
static String className; // Static class variable
// Static block
// Java has no implementation of static constructors, but
// has a static block that can be used to initialize class variables
// (static variables).
// This block will be called when the class is loaded.
static {
className = "Bicycle";
}
// Constructors are a way of creating classes
// This is a constructor
public Bicycle() {
// You can also call another constructor:
// this(1, 50, 5, "Bontrager");
gear = 1;
cadence = 50;
speed = 5;
name = "Bontrager";
}
// This is a constructor that takes arguments
public Bicycle(int startCadence, int startSpeed, int startGear,
String name) {
this.gear = startGear;
this.cadence = startCadence;
this.speed = startSpeed;
this.name = name;
}
// Method Syntax:
// <public/private/protected> <return type> <function name>(<args>)
// Java classes often implement getters and setters for their fields
// Method declaration syntax:
// <access modifier> <return type> <method name>(<args>)
public int getCadence() {
return cadence;
}
// void methods require no return statement
public void setCadence(int newValue) {
9
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void speedUp(int increment) {
speed += increment;
}
public void slowDown(int decrement) {
speed -= decrement;
}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
//Method to display the attribute values of this Object.
@Override // Inherited from the Object class.
public String toString() {
return "gear: " + gear + " cadence: " + cadence + " speed: " + speed +
" name: " + name;
}
} // end class Bicycle
// PennyFarthing is a subclass of Bicycle
class PennyFarthing extends Bicycle {
// (Penny Farthings are those bicycles with the big front wheel.
// They have no gears.)
public PennyFarthing(int startCadence, int startSpeed) {
// Call the parent constructor with super
super(startCadence, startSpeed, 0, "PennyFarthing");
}
// You should mark a method you're overriding with an @annotation.
// To learn more about what annotations are and their purpose check this
// out: http://docs.oracle.com/javase/tutorial/java/annotations/
@Override
public void setGear(int gear) {
gear = 0;
}
}
// Interfaces
// Interface declaration syntax
// <access-level> interface <interface-name> extends <super-interfaces> {
//
// Constants
10
//
// }
// Method declarations
// Example - Food:
public interface Edible {
public void eat(); // Any class that implements this interface, must
// implement this method.
}
public interface Digestible {
public void digest();
}
//
//
//
//
pluto.eat();
pluto.printAge();
}
}
// Final Classes
// Final Class declaration syntax
// <access-level> final <final-class-name> {
//
// Constants and variables
//
// Method declarations
// }
// Final classes are classes that cannot be inherited from and are therefore a
// final child. In a way, final classes are the opposite of abstract classes
// because abstract classes must be extended, but final classes cannot be
// extended.
public final class SaberToothedCat extends Animal
{
// Note still have to override the abstract methods in the
// abstract class.
@Override
public void makeSound()
{
System.out.println("Roar");
}
}
// Final Methods
public abstract class Mammal()
{
// Final Method Syntax:
// <access modifier> final <return type> <function name>(<args>)
// Final methods, like, final classes cannot be overridden by a child class,
// and are therefore the final implementation of the method.
public final boolean isWarmBlooded()
{
return true;
}
}
//
//
//
//
//
//
//
//
Enum Type
An enum type is a special data type that enables for a variable to be a set
of predefined constants. The variable must be equal to one of the values that
have been predefined for it. Because they are constants, the names of an enum
type's fields are in uppercase letters. In the Java programming language, you
define an enum type by using the enum keyword. For example, you would specify
a days-of-the-week enum type as:
Further Reading
The links provided here below are just to get an understanding of the topic, feel free to Google and nd
specic examples.
Ocial Oracle Guides:
14
15