Operations on Data and Java Library

 Operations on Data

1) (byte, short, int, char) + (byte, short, int, char) => int

2) (byte, short, int, long, char) + long => long

3) (byte, short, int, long, float, char) + float => float

4) (byte, short, int, long, float, double, char) + double => double


Note: Operations cannot be performed on boolean types. 

Java Library

  • Java library organised in the form of packages.
  • A package is a collection of sub packages, classes & interfaces.
  • java.lang package is a default package and it is implicitly imported in every java program.

Java Notations:
1) Package & Sub Package: All small letters and sub packages are separated with dot(.) symbol.
2) Class & Interface: Each word first letter is capital and no space between words.
3) Variable & Method: Second word on wards first letter is capital and no space between words.


Interface in Java

  • An interface in Java is a blueprint of a class. It has static constants and abstract methods.
  • The interface in Java is a mechanism to achieve abstraction
  • There can be only abstract methods in the Java interface, not method body. 
  • It is used to achieve abstraction and multiple inheritance in Java.

Example:-

// A simple interface           
interface Player
{
  final int id = 10;
   int move();
}

Java Package

  • A java package is a group of similar types of classes, interfaces and sub-packages.
  • Package in java can be categorized in two form, built-in package and user-defined package.
  • There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.

Example :- import java.util.Scanner


Variables

  • Variable is name of reserved area allocated in memory.
  • In other words, it is a name of memory location. It is a combination of "vary + able" that means its value can be changed.

Types of Variables

There are three types of variables in java :

  • local variable

  • instance variable

  • static variable


Method
  • Method in Java is a collection of instructions that performs a specific task. It provides the reusability of code.
  • A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation.
Types of Method

There are two types of methods in Java:

  • Predefined Method

  • User-defined Method

Eg:- java.io.PrintStream

Simple structure of java

Class Demo 
{
   Public static void main(String args[])
            {
}
}

Simple java program 

Class Demo 
{
   Public static void main(String args[])
{
System.out.println(“welcome”);
}
}

Declaration rules to a source file(.java file):-

1) A source file can have only one public class.
2) A source file can have any number of non public classes.
3) If the source file contains public class then file name and public class name must be same.
4) If the source file does not contain public class then no naming restrictions to a file name.



Comments

Popular posts from this blog

Inheritance in Java

Type Conversions

Life cycle of a Thread and creating thread class