Java – Lesson 1

Imports

  • import java.io.*;

  • import static java.lang.System.*;

  • import java.util.Scanner;

Data Types

  • int – 32 bit
  • long – 64 bit
  • char – single 16 bit unicode character – use a single quote!
  • String

Input / Output

Input

Scanner scan = new Scanner (System.in);

String n = scan.nextLine();

int i = scan.nextInt();

double d = scan.nextDouble();

char c = scan.nextChar();

Output

System.out.print (“My name is ” + name + “\n”);

or

System.out.println (“My name is ” + name);

int birthyear = 1963;
int year = 2013;
System.out.println (“My age in roughly  ”  + (year – birthyear) + ” depending on the date and my birthday” );

Maths

int x;

x++;  /* (x = x + 1) */

x–;  /* (x = x – 1) */

x +=3; /* (x = x + 3) */

x /=3; /* divide x by 3 */

Casting

Java by default assumes that the result of a mathematical expression will be expressed as a datatype that is the same as the first datatype of the expression. 

int a = 15;

int b = 2

double c = a / b; /* C = 7.0; */

double c = (double) a / b; /* we have cast the result to double, so c = 7.5 */

 

Advertisements

About jinkerson
Christian husband and father, rugby fan and computer programmer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: