Sie sind auf Seite 1von 10

A class is a bit of code that contains other bits of code

We'll get to classes in the next chapter. The thing that makes
that class special is the existence of the main method. A
method is a bit of code that does one particular thing in this
case, it starts the program

The class that contains the main method .determines the name
of the program. The name of the program is the name of that
class. For example, the program we write later in this chapter is
called Hello because the class that holds its main method is
named Hello.

SYNTAX
The syntax of any language is the rules that any
speaker of the language follows so that other
speakers of that language can understand. In
other words, syntax is a set of rules that all the
speakers of a language agree to follow so that
they can communicate. If you violate the rules,
people listening to you are either going to ask,
Huh? What? or think you're being silly (which
might be appropriate in some settings but will
often cause a problem).
DATA TYPES
PRIMITIVE DATA TYPES
Primitive means that no class exists to define a variable of that
type. Java supports a number of primitive data types.Ex-
int,boolean.
INTEGER DATA TYPE
Type Bits Minimum Value Maximum Value
byte 8 -128 127
Short 16 -32768 32767
Int 32 -2,147,483,648 2,147,483,647
long 64 -9,223,372,036,854,775,808
9,223,372,036,854,775,807
REAL DATA TYPE-float and double
BOOLEAN
CHAR-The char data type holds two 8-bit bytes and is meant to represent characters. It's stored as
an unsigned 16-bit integer with a minimum value of 0 and a maximum value of 65,535.
STRINGS-String is a type that has some of the characteristics of both a primitive and an object.
Strictly speaking, it is an object; that is, a String class defines it. A String object is a sequence of
characters (and Java provides utilities for turning a String object into a collection of char primitives
and for making a String object from such a collection). It's often handy to work on the collection
(the string) rather than on each character, so Java (and nearly all other programming languages)
provides a String object.
In particular, you can create a String object by using the equals sign (=), and you can concatenate
String objects with the plus sign (+), as shown in Listing 3-1. Concatenation applies only to strings,
by the way. If you use a plus symbol with the other data types, you either get an error
String myString = "my string";
String yourString = "your string";
String ourString = myString + " " + yourString;
System.out.println(myString);
System.out.println(yourString);
System.out.println(ourString);

Literals-All the primitives and the String class can
have literal values. A literal is a constant value
that corresponds to a particular data type.
(Note Declaring a float literal requires appending f
to the end of the literal. Otherwise, the compiler
tries to make it into a double value and then
complains that it can't cast from double to float.
Similarly, you can put d at the end of a double
literal's declaration. However, that is redundant
because double is the default floating-point
type).
ESCAPING CHARACTERS
Escape Sequence Effect
\' Create a single quotation mark
\" Create a double quotation mark
\\ Create a backslash character
\n Create a new line (often called the newline character)
\t Create a tab
\b Create a backspace character (which might delete the
preceding character, depending on the output device)
\r Return to the start of the line (but do not make a new
line)
\f Form feed (move to the top of the next page for printers)
\a The alert (or bell) character
CONVERT ON DATA TYPE TO OTHER
DATA TYPE
// declare an int int myInt = 1;
// and a String that contains a number String myString = "1";
// turn myInt into a String String myIntString = Integer.toString(myInt);
// turn myString into an int int myStringInt = Integer.parseInt(myString);
// turn myString into an Integer
// be prepared for an exception if myString does not hold a number Integer myStringInteger = new
Integer(myString);
// and then turn myStringInteger into an int int myOtherStringInt = myStringInteger.intValue();
// Now for more unusual things
// convert an int to a float (perhaps for further floating-point work) float myFloat = new
Integer(myInt).floatValue();
// convert an int to a byte
// be prepared for an exception if the value is out of byte's range byte myByte = new
Integer(myInt).byteValue();
// convert an int to a long
// no need to worry about an exception this time long myLong = new Integer(myInt).longValue();
// just for fun, get the binary string representation of myInt
// creates a String object that holds "1" String myIntBinary = Integer.toBinaryString(myInt);

ARRAY
An array is a data structure that holds a group of variables under a single identifier. Java supports
arrays for both primitives and objects. Square brackets after a variable's name indicate that it is an
array.
Array of Primitives
int[] a; // array declaration without assignment
a = new int[2]; // specify the length of an existing array
int[] b = {1, 2, 3, 4}; // array declaration with assignment
int bLength = b.length; // how to get the length of an array
// arrays start at 0, not 1 b[0] = 2; // have to reassign each value in the array individually
Let's look at the code one line at a time. The first line creates an array of int primitives but doesn't
assign anything to it (a is null at that point). The second line shows how to set an array to be a
particular length. If a had values, its values would be replaced by the default values of the new type
(0 in this case). The third line shows how to create an array with a set of starting values. You can use
that block assignment syntax only when creating an array, not when assigning new values to an
existing array. The fourth line shows how to get the length of an array. The last line shows how to
reassign one of the values in an array and shows that array addresses start at 0. (Most programming
languages start counting at 0.) te that it is an array.
Listing 3-8. Arrays of objects
Integer[] myIntegers = new Integer[4]; for (int i = 0; i < myIntegers.length; i++) {
myIntegers[i] = new Integer(i); }
Again, let's go line by line. As you can see, the syntax for creating an array of
objects differs a bit from that for primitives. The part to the left of the equal sign
looks the same (the kind of object, the array indicator, and the name of the
variable), but the part to the right of the equal sign differs by having the new
keyword before the type of the item going into the array. To create a new instance
of an object, we usually use the new keyword (though other ways exist), which
calls a constructor for the object. However, all of the Integer class's constructors
require an object (either an int or a String object that holds an integer value), so
this array ends up holding four null references. (We dive into what null means in a
moment.) This listing also shows how to loop through an array and, in this case,
create an object for each of the null objects. Notice that we have to use the new
keyword again, even though we used it when we created the array. Because we
got four null references rather than actual objects from the original assignment,
we have to create new ones here. In this case, we end up with four Integer objects
having values of 0, 1, 2, and 3.

Listing 3-9. Using the Arrays
convenience class
int[] a = {5, 4, 3, 2}; // at the top of the program,
we had to import // java.util.Arrays for this to
work correctly Arrays.sort(a); for (int i = 0; i <
a.length; i++) { System.out.println(a[i]); }
The result is 2, 3, 4, 5 (each on its own line) in the
console.

Das könnte Ihnen auch gefallen