Monday, May 16, 2016

Numbers in Java

To declare and assign a number use the following syntax:
int myNumber;
myNumber = 5;
Or you can combine them:
int myNumber = 5;
To define a double floating point number, use the following syntax:
double d = 4.5;
d = 3.0;
If you want to use float, you will have to cast:
float f = (float) 4.5;
Or, You can use this:
float f = 4.5f (f is a shorter way of casting float)

Characters and Strings

In Java, a character is it's own type and it's not simply a number, so it's not common to put an ascii value in it, there is a special syntax for chars:
char c = 'g';
String is not a primitive. It's a real type, but Java has special treatment for String.
Here are some ways to use a string:
// Create a string with a constructor
String s1 = new String("Who let the dogs out?");
// Just using "" creates a string, so no need to write it the previous way.
String s2 = "Who who who who!";
// Java defined the operator + on strings to concatenate:
String s3 = s1 + s2;
There is no operator overloading in Java! The operator + is only defined for strings, you will never see it with other objects, only primitives.
You can also concat string to primitives:
int num = 5;
String s = "I have " + num + " cookies"; //Be sure not to use "" with primitives.

boolean

Every comparison operator in java will return the type boolean that not like other languages can only accept two special values: true or false.
boolean b = false;
b = true;

boolean toBe = false;
b = toBe || !toBe;
if (b) {
    System.out.println(toBe);
}

int children = 0;
b = children; // Will not work
if (children) { // Will not work
    // Will not work
}

0 comments:

Post a Comment