Reserved words

Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in the first program are public, static, and void.

Modifiers

Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected.  A public datum, method, or class can be accessed by other programs.  A private datum or method cannot be accessed by other programs.

Statements

A statement represents an action or a sequence of actions. For example, the statement

System.out.println("Welcome to Java!")

in the program is a statement to display the greeting “Welcome to Java!” . Every statement in Java ends with a semicolon (;).

Blocks

A pair of braces like these { } in a program forms a block that groups components of a program.

Classes

The class is the essential Java construct. A class is a template or blueprint for objects. A program is defined by using one or more classes.

Methods

What is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is “Welcome to Java!” You can call the same println method with a different argument to print a different message.

The main method

The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method.  The main method looks like this:

public static void main(String[] args) {
  // Statements;
}

Displaying Text in a Message Dialog Box

You can use the showMessageDialog method in the JOptionPane class.  JOptionPane is one of the many predefined classes in the Java system, which can be reused rather than “reinventing the wheel.”

JOptionPane.showMessageDialog(null,
"Welcome to Java!",
"Display Message",
JOptionPane.INFORMATION_MESSAGE);

JOptionPane Input

Two ways of obtaining input.

  1. Using JOptionPane input dialogs
  2. Using the Scanner class (console input)

Getting Input from Input Dialog Boxes

String input =  JOptionPane.showInputDialog( "Enter an input");

Converting Strings to Integers

The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number.  To convert a string into an int value, you can use the static parseInt method in the Integer class as follows:

int intValue = Integer.parseInt(intString);

Where intString is a numeric string such as “123”.

Converting Strings to Doubles

To convert a string into a double value, you can use the static parseDouble method in the Double class as follows:

double doubleValue =Double.parseDouble(doubleString);

Where doubleString is a numeric string such as “123.45”.

A simple java program

DATA Types and Operators

Primitive Data Types

Type Description Size
int The integer type, with range
–2,147,483,648 . . . 2,147,483,647
4 bytes
byte The type describing a single byte, with range
–128 . . . 127
1 byte
short The short integer type, with range
–32768 . . . 32767
2 bytes
long The long integer type, with range –9,223,372,036,854,775,808 . . .
–9,223,372,036,854,775,807
8 bytes
Type Description Size
double The double-precision floating-point type, with a range of about ±10308 and about 17 significant decimal digits 8 bytes
float The single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits 4 bytes
char The character type, representing code units in the Unicode encoding scheme 2 bytes
boolean The type with the two truth values false and true 1 byte

Variables

Named space in memory used to store data of one particular type. The value stored in a variable may change during the execution of a program but the type stored remains the same. A variable has a name that is also called as Identifier for variable. There are some constraints on variable name. we usually call them the naming convention. There are defined below:

  • —  Can be of any length
  • —  Must start with a letter, $ or _.
  • —  Case sensitive i.e one and One are different.
  • —  Cannot have blanks/spaces in identifiers
  • Keywords cannot be used as identifiers

Variable Declaration

Specify the Datatype and give it a name(identifier)

Type identifier [= value][, identifier [= value] ….];

For example:

int  x;
double value, value1=100.50;
char ch='y';

Conversions

Illegal to assign a floating-point expression to an integer variable

double balance =  13.75;
int dollars = balance; // This will generate Error

Casting: used to convert a value to a different type

int dollars = (int) balance; // This is OK

The Math class

Math class contains methods like sqrt and pow. To compute xn, you write Math.pow(x, n). However, to compute x2 it is significantly more efficient simply to compute x * x. To take the square root of a number, use the Math.sqrt; for example, Math.sqrt(x).

Example

If we have the following equation in mathematics.

 

 

 

It can be represented in JAVA as:

Mathematical Methods in Java

Math.sqrt(x) square root
Math.pow(x, y) power xy
Math.exp(x) ex
Math.log(x) natural log
Math.sin(x), Math.cos(x), Math.tan(x) sine, cosine, tangent (x in radian)
Math.round(x) closest integer to x
Math.min(x, y), Math.max(x, y) minimum, maximum
Tagged with: JAVAObject OrientedProgramming
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.