Java-Net Beans IDE- Programming Fundamentals

Tokens

Smallest element of the program known as tokens. They are as follows:


a)    Variables/Identifiers
b)    Literals
c)    Separators/Punctuations
d)    Keywords/Reserved Words
e)    Operators

Variables/Identifiers


A variable is an identifier that denotes a storage location used to store a data value. Unlike constants that remain unchanged during the execution of a program, a variable may take different values at different times during the execution of the program. Or variable is a named storage location in computer memory whose contents can change during a program run.

A variable name can be chosen by the programmer in a meaningful way so as to reflect what it represents in the program.

  • Upper case and Lower case are distinct. This means that the variable total is not ‘equal’ to ‘TOTAL’
§  An identifier is a series of characters consisting of letters, digits, underscore (_) and dollar sign ($) that does not begin with a digit and does not contain any space.

§  Must begin with a letter, dollar sign, or an underscore

§  Are case sensitive

§  Keywords or reserved words can not be used as variable name

Data_type variable_name1, variable_name2; OR 

                              Data_type variable_name1=default value;

Variables can be of following types:

Local Variables: The scope of the local variables is limited to the code level in which they are declared.

Global Variables: Global variables can be referred to anywhere in the code, within any function, whenever it is after its declaration.

Scope of a Variable:
*      The part of program where a variable is usable is called scope of a variable.

Constants/Literals
*      Constants are the fixed value that never change their values during program execution.
final double pi=3.14;
Seprators
*      There are few characters that are used as seperators.
( )         { }        [ ]         ;           ,           .

Keywords/Reserved Words

*      There are some words in Java which have pre-defined meaning to compiler. They implement specific features of the language. Some commonly used keywords are

abstract
generic
super
const
native
var
float
return
catch
int
transient
false
protected
byvalue
import
threadsafe
double
outer
boolean
goto
switch
continue
new
void
for
short
char
interface
true
final
public
case
inner
throw
else
package
break
if
synchronized
default
null
volatile
future
static
class
long
try
finally
rest
cast
instanceof
throws
extends
private
byte
implements
this
do
operator
while


OPERATORS

Operators are the special symbol that performs a particular function on operands. Operators are used in programs to manipulate data and variables. Java operators can be classified into a number of related categories as below:

Arithmetic operators
Arithmetic operators are defined for both integer and floating point numeric types

Operators
Meaning
Example
Result
+
Addition
5 + 4
9
_
Subtraction
5 - 4
1
*
Multiplication
5 * 4
20
/
Division
5 / 2
2
%
Modulus/ Remainder
5 % 2
1


Relational/Comparison operators
Relational operators in java are used to compare two or more variables or constants. All relational operators are Binary Operators, and their operands are numeric expressions.

Operators
Description
= =
Equals to
Greater than
Less than
>=
Greater than equal to
<=
Less than equal to
!=
Not equal to

Logical operators
These operators are used to combine one or more than one relational expressions. By combining logical operators with relational operators. These operators operate on boolean values
Operators
Description
&
Logical AND
&&
Conditional AND
|
OR
||
Conditional OR
!
Logical negation (NOT)

Assignment operators (=)
e.g.      int y =2;
e.g       double x;
            int y;
            x = y; (in this case the value of y is converted into type double, and assign to x)

Unary operators(+, -)
Java provides two unary operators for which only one variable is required.
e.g.      a = -10; b = +20; c = -a; d = + b;
Increment/decrement operators (++, --)
++ or – adds 1 to its operands  subtracts 1
            a = a + 1;         (is the same as ++a; or a++;)
            a = a – 1          (is the same as --a; or a--;)

prefix (++a, --a): these operators follow Change then use
postfix(a++, a--):these operators follow Use then Change            
e.g
Evaluate x = ++y +2 y if y =6
                        = 7 + 2 * 7                  
                        = 21

Conditional/ternary operators (? :)

It requires three operands. The conditional operator is used to replace if – else logic. The syntax of conditional operator is:
Condition ? Statement1 : Statement2;
If the condition evaluates true then the statement1 is evaluated else statement2 is evaluated
e.g.      x = 10;
            y = 15;
            z = (x > y)? x : y;

Order of precedence
If two or more operators are used in an expression, and if there are no parentheses to indicate the order in which the operators are to be evaluated, then the computer needs some way of deciding which operator to evaluate first
Order
Operators
First
* / %
Second
+ -

e.g (1)           8 + 4 * 6 – 8 / 4
  8 + 24 – 8 / 4
    8 + 24 -2
       32 - 2
          30

Data Types

Every variable in Java has a data type. Data type specifies the size and type of value that can be stored. Data types are used to identify the type of data. There are two categories of data type into which data types have been divided:


PRIMITIVE TYPES                                                                            REFERENCE TYPES

integers (byte, short, int, long)                                                                       classes, arrays, interfaces
floating point numbers(float, double)
boolean  (true/false)
character (char)


Type
Storage
Range
boolean
1 bit
True or false
byte
8 bits
-128 to –127
short
16 bits
-32768 to 32767
char
16 bits (2bytes)
0 to 65536
int
32 bits
-2147483648 to 2147483647
long
64 bits
-9223372036854775808 to
9223372036854775807
float
32 bits (4 bytes)
-3.4 E + 38 to 3.4 E + 38
double
64 bits
-1.7 E + 308 to 1.7 E + 308

Strings
String represents a sequence of character. The easiest way to represent a sequence of character.
Note: Java strings can be connected using the ‘+’ operator

Expression
An expression is a formula consisting of one or more operands and zero or more operators linked together to compute a value.

Blocks
A group of statement enclosed in pair of parenthesis {} is called block or a compound statement
{
statements
}


Comments

Comments are the lines in the source code that are ignored by the compiler at during execution of the program. It enhances the readability to the programmer .Comments can be of two types 

Single line comment             e.g.      //
Multiline Comment               e.g.      /* …………………..
                                                            …………………….*/
Type casting/Conversion

Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another explicitly using the cast operator as follows:
(type_name) expression
To assign a value of one type to a variable of another type. If the two types are compatible, then Java will perform the conversion automatically. For example, it is always possible to assign an int value to a long variable.

When one type of data is assigned to another type of variable, an automatic conversion will take place if the following two conditions are met:

Ø  The two types are compatible
Ø  The destination type is larger than the source type

Java provides two types of conversion-

(1)          Implicit Conversion: The conversion performed by the compiler automatically is called implicit conversion
e.g       int x=5
            float y=3.5, z;
            z=x + y;

(2)        Explicit Conversion (Type Casting):          The conversion performed by the user according to their requirements is called explicit conversion
            e.g       int x=5, y=2;
                        float z;
                        z = (float)x / y;

Numeric types are not compatible with char or boolean and char and boolean are not comptible with each other.

Library

A library is a set of readymade classes available with java. A programmer is just required to import (include in the program) a library and simple use the associated classes. e.g. import java.io.*
Using JOptionPane to Display a Message                                                                                                     
You use the JOptionPane class's showMessageDialog method to display a message. The message type argument can be assigned one of these:
  1. JOptionPane.ERROR_MESSAGE
  2. JOptionPane.INFORMATION_MESSAGE
  3. JOptionPane.WARNING_MESSAGE
  4. JOptionPane.QUESTION_MESSAGE
  5. JOptionPane.PLAIN_MESSAGE (no icon will be used)
For example, the following code displays four different JOptionPane dialogs.
JOptionPane.showMessageDialog(null,"Message","Title",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Message","Title",JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(null,"Message","Title", JOptionPane.ERROR_MESSAGE);
Using Input Dialogs
String input = JOptionPane.showInputDialog("Enter Input:");
System.out.println(input);


Commonly available Swing Controls in Java

jFrame: A Frame is a container control, in which all the controls can be place.
jLabel: JLable allows placing un-editable text on the Frame/Panel during run time
jTextField: JTextFiled allows placing editable text on the Frame/Pane. User can enter text in a textField during runtime.
jbutton: is used to initiate an action when it is clicked.
jList: is a group of values or items from which one or more selections can be made.
jComboBox: jComboBox is similar to jList but also allow to enter editable text during run time. It is a combination of jTextFiled and jList.
jPanel: Act like a frame, to group one or more controls.
jRadioButton: Allow us to choose a single item from a group of jRadioButton options.
jCheckBox: Allow us to choose one or more items from a group of jCheckBox options.
jPasswordField: Allow us to enter a text during the run time but shows an encrypted text instead of the original text
jTextArea: JTextArea is a multi-line text component to enter or edit text.

Comments

Popular posts from this blog

Computer Networking

Python-NumPy

MySQL Simple Queries