Vous êtes sur la page 1sur 5

Syntax to print text:

System.out.println(text);

Data types

Primitives
Bytes
-128 to 127

Integer 32 bit
-2.147 bil to 2.147 bil
Long 64 bits
-9223 tril to 9223 tril
Float single precision 32 bit
Very big

Double 64 bit
Huge
Double precision floating point
Can handle decimals

Commonly used
Ints and doubles

Char
Short for characters
16 bit Unicode character
Letters, numbers, brackets, periods, etc are all characters

To store an integer
Int y = 3;
Now y is equal to 3
Putting a decimal will make a compiler error, for ex
Int y = 3.2

To cast a number into an integer


Int y=(int)3.2
This will print 3. The (int) part truncates the until the largest integer value. NOT
rounding up

Double data types allow integers


Double x = 4.77
Printing this will display 4.77.

Shortcut for self-referencing operations


x=x+5

is the same as
x+=5

similarly,
- x/=5
is the same as
- x=x/5
the same is true for -, and *
this is useful when the names of the variables are long
- mydouble = mydouble-5
takes much longer to type than
- mydouble-=5

Stores a bunch of objects

Example of array for integers


- int[] myarray = new int[5]
stores 5 integers

sample whole coed for using arrays

public class stuff {

public static void main(String[] args) {

int[] myarray = new int[5];


myarray[0] = 12;
System.out.println(myarray[0]);
System.out.println(myarray[3]);
}

Running this will display 12 and 0 since an array automatically assigns a value of 0
to each of its members.

Example of code for assigning values for strings

public class stuff {

public static void main(String[] args) {

String mystring = "Hello world!";


System.out.println(mystring);
}

Running this will display Hello world!

Note: You cannot use keywords as variable names. Some keywords are: false,
true, for and if. Anything in purple is a keyword Also you cannot put numbers
at the beginning of your variable names. Ex. a1423 is a valid variable name but
1232 is not.

For Booleans
public class stuff {
public static void main(String[] args) {

boolean mybool = true;


System.out.println(mybool);

They can only take values of either true or false

For charaters

public class stuff {

public static void main(String[] args) {

char a2a = 'a';


System.out.println(a2a);

Prints out a only


Note: double quotation marks ( ) are for strings while single quotation marks ( )
are for characters.

Importing a package util


The code creates a scanner object in

import java.util.*;
public class stuff2 {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.println("Welcome! Please enter your name!");
String username = in.nextLine();
System.out.println("Please enter the passcode of user " + username);
int passcodeFromUser=in.nextInt();
int passcode = 7001;
if (passcodeFromUser == passcode)
{
System.out.println("Welcome " + username +"!");
}
else
{
System.out.println("Access denied");
}

import java.util.*;
public class stuff2
{

public static void main(String[] args)


{
Scanner rin = new Scanner(System.in);
System.out.println("Welcome! Please enter your name!");
String username = rin.nextLine();
System.out.println("Please enter the passcode of user " + username);
int passcodeFromUser=rin.nextInt();
int passcode = 7001;
if (passcodeFromUser == passcode)
{
System.out.println("Welcome " + username +"!");
Random random = new Random(username.hashCode());
System.out.println("Here are your random numbers");
System.out.println(random.nextInt(100) + "," +
random.nextInt(100) + "," +
random.nextInt(100));
}
else
{
System.out.println("Access denied");
Random random = new Random(username.hashCode());
System.out.println("Here are some weird random numbers anyway...");
System.out.println(random.nextInt(5)+10);
}

rin.close();
}

Note that random.nextInt(5)+10 generates a random number from 10 to 14.

First the random.nextInt(5) generates random numbers from the set{0,1,2,3,4} then increments each
by 10 afterwards.

Vous aimerez peut-être aussi