Tuesday, 30 August 2016
Monday, 29 August 2016
Aggregation in Java
Aggregation in Java
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
Consider a situation, Employee object contains many informations such as id, name, emailId etc. It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.
- class vipul{
- int id;
- String name;
- Address address;//Address is a class
- ...
- }
When use Aggregation?
- Code reuse is also best achieved by aggregation when there is no is-a relationship.
- Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved.
- aggregation is the best choice.
Example
- class one{
- int square(int n){
- return n*n;
- }
- }
- class two{
- Operation op;//aggregation
- double pi=3.14;
- double area(int radius){
- op=new one();
- int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
- return pi*rsquare;
- }
- public static void main(String args[]){
- two c=new two();
- double result=c.area(5);
- System.out.println(result);
- }
- }
Sunday, 28 August 2016
Inheritance in Java
Inheritance in Java
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Syntax
public class vipul
{
}
public class Agravat extends vipul
{
}
The extends keyword indicates that you are making a new class that derives from an existing class.
In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.
class vipul{
float salary=200;
}
class Agravat extends vipul
{
int bonus=100;
public static void main(String args[]){
Agravat A=new Agravat();
System.out.println("Programmer salary is:"+A.salary);
System.out.println("Bonus of Programmer is:"+A.bonus);
}
}
OUTPUT
Programmer salary is:200
"Bonus of Programmer is:100
Saturday, 27 August 2016
This keyword in java
This keyword in java
There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.
Usage of java this keyword
Here is given the 6 usage of java this keyword.
- this keyword can be used to refer current class instance variable.
- this() can be used to invoke current class constructor.
- this keyword can be used to invoke current class method (implicitly)
- this can be passed as an argument in the method call.
- this can be passed as argument in the constructor call.
- this keyword can also be used to return the current class instance.
If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.
Friday, 26 August 2016
Java Naming conventions, CamelCase
Java Naming conventions
Java naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method etc.
But, it is not forced to follow. So, it is known as convention not rule.
All the classes, interfaces, packages, methods and fields of java programming language are given according to java naming convention.
Advantage of naming conventions in java
By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.
class name
---------->should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
interface name
--------------->should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
method name
--------------->should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
variable name
----------------->should start with lowercase letter e.g. firstName, orderNumber etc.
package name
------------------>should be in lowercase letter e.g. java, lang, sql, util etc.
constants name
----------------->should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
CamelCase in java naming conventions
Java follows camelcase syntax for naming the class, interface, method and variable.
If name is combined with two words, second word will start with uppercase letter always e.g. actionPerformed(), firstName, ActionEvent, ActionListener etc.
Thursday, 25 August 2016
OOP(Object Oriented Programming System)
OOPs (Object Oriented Programming System)
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Object
Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike,Car etc. It can be physical and logical.
Class
Collection of objects is called class. It is a logical entity class is also the name start with class.
class is a an Entity.exmple
public class vipul
{
}
Inheritance
When one object acquires all the properties and behaviours of parent objecti.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Example
public class vipul
{
}
public class agravta extends vipul
{
}
Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc.
In java, we use method overloading and method overriding to achieve polymorphism.
================overriding =======================
public class vipul
{
public void a(){
}
}
public class Agravat
{
public void a();
}
================overloading =======================
public class vipul
{
public void a(int a)
{
}
}
public class Agravat
{
public void a(int a,int b){
}
}
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing.
In java, we use abstract class and interface to achieve abstraction.
abstraction class vipul
{
}
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
Tuesday, 23 August 2016
javadoc tools or API Document
Creating API Document | javadoc tool
We can create document api in java by the help of javadoc tool. In the java file, we must use the documentation comment /**... */ to post information for the class, method, constructor, fields etc
package com.agravatvipul;
/** This class is a user-defined class that contains one methods cube.*/
public class vip
{
/** The cube method prints cube of the given number */
public static void cube(int n)
{System.out.println(n*n*n);
}
}
To create the document API, you need to use the javadoc tool followed by java file name. There is no need to compile the javafile.
On the command prompt, you need to write:
javadoc vip.java
to generate the document api. Now, there will be created a lot of html files. Open the index.html file to get the information about the classes.
Sunday, 21 August 2016
JAVA UNICODE SYSTEM
Why java uses Unicode System?
unicode system that use to encoded the byte code .
Before Unicode, there were many language standards: |
|
This caused two problems:
|
To solve these problems, a new language standard was developed i.e. Unicode System. |
In unicode, character holds 2 byte, so java also uses 2 byte for characters. |
lowest value:\u0000 |
highest value:\uFFFF |
Saturday, 20 August 2016
Different java (JVM) (Java Virtual Machine),(JER)java runtime environment,(JDK)Java Development Kit
Difference between JDK, JRE and JVM
Understanding the difference between JDK, JRE and JVM is important in Java. We are having brief overview of JVM here.
If you want to get the detailed knowledge of Java Virtual Machine, move to the next page.
JVM
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment. it also bytecode can be executed.
JVMs are available for many hardware and software platforms. JVM platform dependent because configuration of each OS differs. But, Java is platform independent.
The JVM performs following main tasks:
- Loads code
- Verifies code
- Executes code
- Provides runtime environment
JRE
It is used to provide runtime environment.It is the implementation of JVM. It physically exists. It contains set of libraries + other files that JVM uses at runtime.
Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.
JDK
JDK is an acronym for Java Development Kit.It physically exists.It contains JRE + development tools.
Friday, 19 August 2016
Wednesday, 17 August 2016
java and c++
Explain Different between c++ and java
Comparison Index | C++ | Java |
---|---|---|
Platform-independent | C++ is platform-dependent. | Java is platform-independent. |
Mainly used for | C++ is mainly used for system programming. | Java is mainly used for application programming. It is widely used in window, web-based, enterprise and mobile applications. |
Goto Statement | C++ supports goto statement. | Java doesn't support goto statement. |
Multiple inheritance | C++ supports multiple inheritance. | Java doesn't support multiple inheritance through class. It can be achieved by interfaces in java. |
Operator Overloading | it supports operator overloading. | it doesn't support operator overloading. |
Pointers | C++ supports pointers. You can write pointer program in C++. | Java supports pointer internally. But you can't write the pointer program in java. It means java has restricted pointer support in java. |
Compiler and Interpreter | C++ uses compiler only. | Java uses compiler and interpreter both. |
Call by Value and Call by reference | C++ supports both call by value and call by reference. | Java supports call by value only. There is no call by reference in java. |
Structure and Union | C++ supports structures and unions. | Java doesn't support structures and unions. |
Thread Support | C++ doesn't have built-in support for threads. It relies on third-party libraries for thread support. | Java has built-in thread support. |
Documentation comment | C++ doesn't support documentation comment. | Java supports documentation comment (/** ... */) to create documentation for java source code. |
Virtual Keyword | C++ supports virtual keyword so that we can decide whether or not override a function. | Java has no virtual keyword. We can override all non-static methods by default. In other words, non-static methods are virtual by default. |
unsigned right shift >>> | C++ doesn't support >>> operator. | Java supports unsigned right shift >>> operator that fills zero at the top for the negative numbers. For positive numbers, it works same like >> operator. |
Tuesday, 16 August 2016
Operators in java
Operators in java
Operator in java is a symbol that is used to perform operations. There are many types of operators in java such as unary operator, arithmetic operator, relational operator, shift operator, bitwise operator, ternary operator and assignment operator.
|
Monday, 15 August 2016
What is java and Types of Java Applications
What is java and Types of Java Applications
java is Programing Language and they also called ans oak and it produce at 1995.
There are mainly 4 type of applications that can be created using java programming:
1) Standalone Application
2) Web Application
3) Enterprise Application
4) Mobile Application
1) Standalone Application
It is also known as desktop application or window-based application. An application that we need to install on every machine such as media player, antivirus etc. AWT and Swing are used in java for creating standalone applications.2) Web Application
An application that runs on the server side and creates dynamic page, is called web application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web applications in java.3) Enterprise Application
An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In java, EJB is used for creating enterprise applications.4) Mobile Application
An application that is created for mobile devices. Currently Android and Java ME are used for creating mobile applications.Friday, 12 August 2016
Java Keyword in Details
Java KeyWords?
Used to define a block of statements for a block defined previously by the try keyword. The finally block is executed after execution exits the try block and any associated catch clauses regardless of whether an exception was thrown or caught, or execution left method in the middle of the try or catch blocks using the return keyword.
As of J2SE 5.0, the for keyword can also be used to create a so-called "enhanced for loop",[14] which specifies an array or Iterable object; each iteration of the loop executes the associated block of statements using a different element in the array or Iterable.[13]
implements
Included in a class declaration to specify one or more interfaces that are implemented by the current class. A class inherits the types and abstract methods declared by the interfaces.
strictfp (as of J2SE 1.2)
A Java keyword used to restrict the precision and rounding of floating point calculations to ensure portability.[10]
Also used to specify a lower bound on a type parameter in Generics.
char
Defines a character variable capable of holding any character of the java source file's character set (NB: Physical storage may exceed one byte).class
A type that defines the implementation of a particular kind of object. A class definition defines instance and class fields, methods, and inner classes as well as specifying the interfaces the class implements and the immediate superclass of the class. If the superclass is not explicitly specified, the superclass is implicitly Object. The class keyword can also be used in the form Class.class to get a Class object without needing an instance of that class. For example, String.class can be used instead of doing new String().getClass().
const
Although reserved as a keyword in Java, const is not used and has no function.[2][1] For defining constants in java, see the 'final' reserved word.continue
Used to resume program execution at the end of the current loop body. If followed by a label, continue resumes execution at the end of the enclosing labeled loop body.default
The default keyword can optionally be used in a switch statement to label a block of statements to be executed if no case matches the specified value; see switch.[3][4] Alternatively, the default keyword can also be used to declare default values in a Java annotation. From Java 8 onwards, the default keyword is also used to specify that a method in an interface provides the default implementation of an optional method.do
The do keyword is used in conjunction with while to create a do-while loop, which executes a block of statements associated with the loop and then tests a boolean expression associated with the while. If the expression evaluates to true, the block is executed again; this continues until the expression evaluates to false.double
The double keyword is used to declare a variable that can hold a 64-bit double precision IEEE 754 floating-point number. This keyword is also used to declare that a method returns a value of the primitive type double.else
The else keyword is used in conjunction with if to create an if-else statement, which tests a boolean expression; if the expression evaluates to true, the block of statements associated with the if are evaluated; if it evaluates to false, the block of statements associated with the else are evaluated.enum
A Java keyword used to declare an enumerated type. Enumerations extend the base class Enum.extends
Used in a class declaration to specify the superclass; used in an interface declaration to specify one or more superinterfaces. Class X extends class Y to add functionality, either by adding fields or methods to class Y, or by overriding methods of class Y. An interface Z extends one or more interfaces by adding methods. Class X is said to be a subclass of class Y; Interface Z is said to be a subinterface of the interfaces it extends.final
Define an entity once that cannot be changed nor derived from later. More specifically: a final class cannot be subclassed, a final method cannot be overridden, and a final variable can occur at most once as a left-hand expression on an executed command. All methods in a final class are implicitly final.
finally
Used to define a block of statements for a block defined previously by the try keyword. The finally block is executed after execution exits the try block and any associated catch clauses regardless of whether an exception was thrown or caught, or execution left method in the middle of the try or catch blocks using the return keyword.
float
The float keyword is used to declare a variable that can hold a 32-bit single precision IEEE 754 floating-point number.[7][8] This keyword is also used to declare that a method returns a value of the primitive type float.[9][10]for
The for keyword is used to create a for loop, which specifies a variable initialization, a boolean expression, and an incrementation. The variable initialization is performed first, and then the boolean expression is evaluated. If the expression evaluates to true, the block of statements associated with the loop are executed, and then the incrementation is performed. The boolean expression is then evaluated again; this continues until the expression evaluates to false.[13]As of J2SE 5.0, the for keyword can also be used to create a so-called "enhanced for loop",[14] which specifies an array or Iterable object; each iteration of the loop executes the associated block of statements using a different element in the array or Iterable.[13]
goto
Although reserved as a keyword in Java, goto is not used and has no function.if
The if keyword is used to create an if statement, which tests a boolean expression; if the expression evaluates to true, the block of statements associated with the if statement is executed. This keyword can also be used to create an if-else statement; see else.implements
Included in a class declaration to specify one or more interfaces that are implemented by the current class. A class inherits the types and abstract methods declared by the interfaces.
import
Used at the beginning of a source file to specify classes or entire Java packages to be referred to later without including their package names in the reference. Since J2SE 5.0, import statements can import static members of a class.instanceof
A binary operator that takes an object reference as its first operand and a class or interface as its second operand and produces a boolean result. The instanceof operator evaluates to true if and only if the runtime type of the object is assignment compatible with the class or interface.int
The int keyword is used to declare a variable that can hold a 32-bit signed two's complement integer.[ This keyword is also used to declare that a method returns a value of the primitive type int.interface
Used to declare a special type of class that only contains abstract methods, constant (static final) fields and static interfaces. It can later be implemented by classes that declare the interface with the implements keyword.long
The long keyword is used to declare a variable that can hold a 64-bit signed two's complement integer.This keyword is also used to declare that a method returns a value of the primitive type long.[native
Used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language.new
Used to create an instance of a class or array object. Using keyword for this end is not completely necessary (as exemplified by Scala), though it serves two purposes: it enables the existence of different namespace for methods and class names, it defines statically and locally that a fresh object is indeed created, and of what runtime type it is (arguably introducing dependency into the code).package
A group of types. Packages are declared with the package keyword.private
The private keyword is used in the declaration of a method, field, or inner class; private members can only be accessed by other members of their own class.[15]protected
The protected keyword is used in the declaration of a method, field, or inner class; protected members can only be accessed by members of their own class, that class's subclasses or classes from the same package.public
The public keyword is used in the declaration of a class, method, or field; public classes, methods, and fields can be accessed by the members of any class.return
Used to finish the execution of a method. It can be followed by a value required by the method definition that is returned to the caller.short
The short keyword is used to declare a field that can hold a 16-bit signed two's complement integer.[ This keyword is also used to declare that a method returns a value of the primitive type short.static
Used to declare a field, method, or inner class as a class field. Classes maintain one copy of class fields regardless of how many instances exist of that class. static also is used to define a method as a class method. Class methods are bound to the class instead of to a specific instance, and can only operate on class fields. (Classes and interfaces declared as static members of another class or interface are actually top-level classes and are not inner classes.)strictfp (as of J2SE 1.2)
A Java keyword used to restrict the precision and rounding of floating point calculations to ensure portability.[10]
super
Used to access members of a class inherited by the class in which it appears. Allows a subclass to access overridden methods and hidden members of its superclass. The super keyword is also used to forward a call from a constructor to a constructor in the superclass.Also used to specify a lower bound on a type parameter in Generics.