Java Syntax And Class Review

Java Syntax And Class Review

                                                                Class Structure

package <package_name>;

import <other_packages>;

public class ClassName{
<variables(also known as fields)>;

<constructor method(s)>;

<other methods>;

}

A Java class is described in a text file with a .java  extension. In this example shown, the java keywords are highlighted in bold.

  • The package keyword defines where this class lives relative to other classes, and provides a level of access control.
  • The import keyword defines other classes or groups of classes that you are using in your class. The import statement helps to narrow what the compiler needs to look for when resolving class names used in this class.
  • The class keyword precedes the name of this class. the name of the class and the file name must match when the class is declared public(which is a good practice)However, the keyword public in front of the class keyword is a modifier and is not required.
  • Variables, or the data associated with programs ( Such as integers , strings, arrays)and references to other objects ), are called instance fields (often shortened to  fields).
  • Constructor are functions called during the creation (instantiation)of an object (a representation in memory of a Java class).
  • Methods are the functions that can be performed on an object. They are also referred to as instance methods.
   A Simple Class

A Simple Class with a main method.

public class Simple{

public static void main(String[] args){

}

}



To run  a Java program, you must define  a main method. The main method is automatically called when the class is called from the command line. Command-line arguments are passed to the program through the arr[] array.




Code Blocks


  • Every class declaration is enclosed in a code block.
  • Method declarations are enclosed in code blocks.
  • Java fields and methods have block (or class) scope.
  • Code blocks are defined in braces.

  • Example

Java fields ( variables)  and methods have a class scope defined by the opening left curly brace and ending at the closing right curly brace.

Class scope allows any method in the class to call or invoke any other method in the class. Class scope also allows any method to access any field in the class.


Primitive Data Types


                                                                          Integer

Java provide four different integer types to accommodate different size numbers. All the numeric type are signed , which means that they can hold positive or negative numbers.  
The integer types have the following ranges:

  • byte range is -128 to +127. Number of bits= 8.
  • short range is -32,768 to +32,767. Number of bits=16.
  • int range is -2,147,483,648 to 2,147,483,647. The most common integer type is int. Number of bits= 32.
  • long range is -9,223,372,036,854,775,808 to + 9,223,372,036,854,775,807. Number of bits=64.
                                                                        Boolean

Boolean type can hold either true or false.

                                                                   Floating Point

The floating-point types hold  numbers with a fractional part and conform to IEEE 754 standard. There are two types of floating points: float and double.

double is so called because it provides double the precision of  float. A  float uses 32 bits to store data , whereas a double uses 64 bits.

Character

The char type is used for individual characters , as opposed to a string of characters( which is implemented as a String object). Java supports Unicode, an international standard for representing  a character in any written language in the world in a single 16 bit value. The first 256 characters coincide with the ISO Latin 1 character set, part of which is ASCII.

Default Values

If a value is not specified , a default value is used The value in red in the slide are the defaults used . The default char value is null( represented as '\u0000), and the default value for boolean is false.

About Codingwelove

Codingwelove
Recommended Posts × +

0 comments:

Post a Comment