Home Java Java-tips Summaries Summary - Basic Elements

Ask Questions?

View Latest Questions


 
 

Summary - Basic Elements
Posted on: July 27, 2006 at 12:00 AM
Basic Elements

Java: Summary - Basic Elements

In this section we will learn about commenting in Java.

We will learn following things:

a) Identifiers in Java.

b) Object types in Java.

c) Code commenting in Java

d) Integer types in Java

e) Keywords in Java.

Following table shows all these things with relevant description.

Comments

// Everything to the end of the line is ignored.
/* Everything (possibly many lines) is ignored until a */
/** Used for automatic HTML documentation generation by the javadoc program. */

Types

Types are divided into two categories. There are 8 primitive types: boolean (true and false); char (Unicode characters); byte, short, int, and long (integers); float and double (floating point). Object types are created whenever a class is defined.

Integer Types

The integer types byte, short, int, and long are stored as two's complement, signed binary integers. char, which is technically also an integer type, is stored as an unsigned binary number. Expressions are computed as ints, so a cast is needed to store in a smaller type.
Type BytesRange Literals
byte 1-128..+127 none
short2-32,768..+32,767 none
int 4-2,147,483,648..+2,147,483,64723, 0xAF
long 8-9,223,372,036,854,775,808.. 23L, 0xAFL
Operators: arithmetic, comparison, bitwise, assignment.

Floating-point Types

The floating-point types, float and double, are stored in IEEE-754 format. Calculations may produce NaN (Not a Number) or +/- infinity. Calculations are done as doubles, so a cast is needed to store in a float.
Type Bytes Range Accuracy Literals
float 4-3.4E38..+3.4E38 6-7 digits 3.14F 6.02e23F
double8-1.7E308..+1.7E30814-15 digits3.14 6.02e23
Operators: arithmetic, comparison, assignment.

char Type

char type is a Unicode character stored as an unsigned number in two bytes (range 0..65,535 or '\u0000'..'\uFFFF'). char is an integer type.
Literals
  • 'A' (single character)
  • Unicode '\uxxxx' where x is a hexadecimal digit. Eg '\u0041'
  • Octal '\nnn' where n is an octal digit.
  • Escape combinations: '\n' newline, '\\' backslash, '\'' single quote, '\"' double quote, '\r' carriage return, '\t' tab, '\b' backspace, '\f' form feed.

boolean Type

boolean is used to store the values true or false. The storage is unspecified.
Operators: logical, ==, !=, assignment.

Identifiers

  • Start identifiers with an alphabetic character (a-z or A-Z), and continue with alphabetic, numeric (0-9), or '_' (underscore) characters. Do not use $.
  • Second words in a name should start with an uppercase letter.
  • Do not use keywords (see below).
  • Class and interface names should start with an uppercase letter (Graphics, ActionListener, JButton, ...)
  • Variable and method names should start with a lowercase letter (repaint(), x, ...).
  • Constants should be all uppercase with underscores between words (BoxLayout.X_AXIS, Math.PI, ...).

Keywords

[types] boolean byte char double float int short true false [flow] if else while for do continue switch case break default assert try catch finally throw return [OOP] class extends implements instanceof interface new null super this [declarations] final import native package private protected public static throws transient volatile void [other] strictfp synchronized goto const

Variables

Variables may be local, instance (field), or static (class) variables. Formal parameters are local variables that are assigned values when the method is called.
local instance static / class
Where declared In a method. In class, but not in a method. In class using static keyword.
Initial value Must assign a value before using. Zero for numbers, null for objects, false for boolean. Can be initialized in constructor. Zero/null/false or initialized with static initializer.
Visibility Only in the same method. private: Only methods in this class. Default: All methods in same package. public: Anyone can see it. Same as instance.
Created When method is entered. When an instance of the class (object) is created with new. When program is loaded.
Destroyed When the method returns. When there are no more references to the object. When program terminates.

Copyright 2004 Fred Swartz Last update: 2004-08-18

Related Tags for Summary - Basic Elements:
javahtmlccomideclassdocumentationunicodedivobjectcommentsiotypesfloatintegersedtypeadocharbooleanjavadocdoubleviolebyteintlineidoocharactercreatecharactersdefinebooforpoiielongprimitivecteignoreprogramtopointautomaticramgenerationcommentlinesposboolssishintegersautocategorieseilitdoclslimituseshortpeimmaninnomlasmnttrthinnicoscaosnicleanjadclestruefalseendicomeintoobjprocatdodefinedwhenscreatedatanyracratioosshtmishaivomagodivideeaandaractcodcodertwhenevervatwssrithshoavatifinmanyjeprmindodeonomogrolonor