Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, 10 May 2018

Chaudhari Karan

Java - Objects and Classes

By     No comments:


Lecture 3  Java – Objects and Classes


Hello friends, today we will discuss chapter Objects and Classes.

Objects and Class in java

Java has some object-oriented feature and some fundamental concepts:
  • ·         Abstraction
  • ·         Classes
  • ·         Encapsulation
  • ·         Inheritance
  • ·         Instance
  • ·         Method
  • ·         Massage Parsing
  • ·         Objects
  • ·         Polymorphism

In this chapter, we will discuss Objects and Classes concepts.

Objects: In java object is states and behavior. Example: A cat has states – color, name, etc. and behavior – eating, walking, etc.

Classes: Class describes the state and behavior of objects.

Objects in Java :

Now, we look objects in deep if we look around us, we find many objects which have states and behavior like cars, humans, dogs, etc.

If we consider the car as an object so the car has state – color, name, etc. and its behavior – speed, running, etc.

If we compare objects with software, so find the very similarity.

If we consider software as an object, so it has states and behavior. Like in software object state is stored files and behavior is methods.

How to create an object in Java :

In java object is created from the class. For create objects new keyword used in java.

Here is an example of object :

Test t = new Test();  
Hello h = new Hello();

In the 1st  example, I want to create Test class object. So used the Test as variable name and t is reference variable. After that used the new keyword for the object and variable name. 

Here is practical of objects creating :

Object

Output: Passed name is: Tommy

Classes in Java :

The class is state and behavior of Object.

Example of the class:

The class contains any type of variable.

Class


Local variable: Variable is defined inside constructor and methods are known as the local variable. A variable is declared and initialized in method and it destroys after completed.

Class variable: Variable is declared with the name outside the method, with any static keyword is called the Class variable :

Instance variable: Variable with a class but it is outside the method. These variable accesses from inside any constructor and method.

The class has the number of methods to access the various type of methods. Example, sleeping(), running(), eating().

Constructor: 


We discussing class. The class subtopic is the constructor. Every class has a constructor. In Java, if we do not create explicitly constructor for a class, so java compiler creates the default constructor for that class.

Every time created the new object, at least one constructor will be invoked. The main rule of the constructor is they should have the same name of the class. A class has more than one constructor.

Example of a constructor :

Constructor

Java supports Singleton Classes. Where able to create to only one instance class.

How to use Singleton Class:


Singleton Classes purpose is to control object creation and limiting the number of objects to only one. There has only one Singleton instance class, any instance field occurs only once per class, like the static field.

For example of Singleton Class, If you have only one connection to your database has trouble from multithreading, the Singleton makes only one connection or thread which can access connection at a time.


Implementing Singleton:


Example 1

Singleton Class

Output: demoMethod for singleton

Example 2

Following program shows the implementation of classic singleton design pattern.

Singleton class

ClassicSingleton class maintain a static reference to the long singleton instance and return the reference from static getInstance() method.

ClassicSingleton class employs a technique which called as instantiation to create Singleton class. Singleton instance is not created until getInstance() method. 

Accessing Instance Variable and Methods:


An instance variable is accessed by the creating the object. To access instance variable following is fully qualified path :

Instance variable


Example:

In this example explain how to access instance variable and method of the class.

Instance variable

Output:  

Name chosen is: Tommy
Puppy's age is: 2
Variable Value: 2

Source file declaration rules :

Here we discuss some declaration rules. It is essential in classes, import statement, and package in the source file.

  • ·         There is only one public class in one source file.
  • ·         Source file has multiple non-public classes.
  • ·         The public class name is source file name which is appended with .java Example: public class name Puppy so source file name is Puppy.java
  • ·         If the class is defined inside the package, then package statement first statement in the source file.
  • ·         If import statement is present in the file, then they were written between the package statement and class declaration. If there is not present the package statement then import statement is the first line in the source file.
  • ·         Package and Import statements will imply to all present classes in the source file. It is not possible to declare different package and import statements in different classes in the source file.

Package in Java :


Java package is a categorizing the interfaces and classes. When developing an application in java there is package has hundreds of classes and interfaces which it makes much easier.

Import in Java :


In Java, Import statement is used for Import some file and classes.
Example: import java.io.*;

Next: In next lecture, we will discuss basic data types in Java.
Read More

Sunday, 6 May 2018

Chaudhari Karan

Java basic syntax, how to compile program in java

By     No comments:

Lecture 2  Java – Basic Syntax, How to compile program in java

Hello friends, In today lecture we will discuss some basic syntax in java. 

Java basic syntax
Java is a collection of Objects. Before we start you have to know about Object, Class, Instance Variables, and Methods.

Object: Java object is states and behaviors. The object is the instance of a class. Example: A Dog has state  - name, and color, etc. and its behavior – eating, barking, etc.

Class: A class can be defined as it is a collection of state and behaviors.

Instance Variables: Every object has the unique set of the instance variable. For object state created by assigned value of these instance variable.  

Methods: Basically a method is a behavior. A class contains many other methods. Which methods logic is written and data is manipulated and the actions are executed.

Program: Print "Hello World" in java.


Java Program



How to compile and run java program :


Step 1: Open Notepad or Notepad++ and write the program as above.

Step 2: Save the program or file as MyFirstJavaProgram.java, In java file always save with .java extension.

Here most common thing you have to know that program or file save with the name of the main class. In above program, the main class is MyFirstJavaProgram and file save with the MyFirstJavaProgram.java

Step 3: Open the command prompt and go to the directory where you saved your file.


Step 4: Compile the file: javac MyFirstJavaProgram.java

java command prompt


Step 5: After compile run the file: java MyFirstJavaProgram

java command prompt


Output: Hello World

Basic Syntax :


Case Sensitivity: Java is case sensitive, which means like Identifier Hello and hello both meanings is different in Java.

Class Name: In java class name should be in Upper Case latter. If several words used in class so each inner world should be in Upper Case latter. Example: class MyFirstJavaProgram

Program File: In java file name always save with the class name. If the filename and class name would not be same so your program will not be compiled. Example: assume the class name is 
Example. So file save with the Example.java

Methods Name: Each method name should start with the Lower Case letter. If several words are used in the method so each inner world should be in Upper Case. Example : public void myFirstName()

Main Function: Every java program processing start from the main function or method.
public static void main(String [] args)

The identifier in Java :


In Java, every component requires the name. so name used for class, object, variable, and method are known as Identifier.

Here have some points for Identifier :

  • Each identifier begins with the letter (A to Z or a to z), currency character ($), or underscore(_).
  • Identifier cannot be start with any number(12) or operator(+,-,*,/).
  • The identifier is a combination of characters.
  • Keyword not used as an Identifier.
  • An identifier is case sensitive.
  • Legal Identifier : test, $age, _example, _5_name.
  • Illegal Identifier : 25example, +age.

The modifier in Java :


Two types of modifier in java.
  • Access Modifier: public, private, protected, and default.
  • Non-access Modifier: abstract, final strictfp

We will discuss modifier in detail in Modifier chapter.

Variable in Java :


There are some types of variables.
  • Local Variable
  • Class Variable or Static Variable
  • Instance Variable or Non-static Variable

Array in Java :


Arrays are used for store the multiple variables of the same type. An array is an object.

Enums in Java :


Enums introduced in Java 5.0. Enums have one of only a few predefined values. That values in this enumerated list are known as enums.

For reducing the number of bugs in code you can use the enums in your code.

java program


Output: Size: Large

Keyword in Java :


abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while






Comments in Java :

java program

Java supports single line and multiline comments. It is very similar to C and C++. All character inside the comment is ignored by the compiler.

Using Blank Line in Java :


In Java, a line containing only whitespace, possibly with the comment, so it is known as blank line, and java ignores the blank line.

Inheritance :


In java class is derived from the classes. So, if you create a new class and here has already a class that has some code but you need that classes code for your new class. In this situation, you can use the concept of Inheritance.

Inheritance through you can use the code of that class for your new class. You don't write the code again. Example, the existing class is super class and the new class is a subclass(derived class).

Interfaces :


In Java, the interface can be defined as the construct between object can communicate with each other.

Next :  In next lecture discuss chapter Objects and classes.

 


Read More

Saturday, 5 May 2018

Chaudhari Karan

Java Introduction, How to download java and how to set path in java.

By     No comments:

Lecture 1 Java –Introduction


Hello, friends

Here we start java programming language. We will consider the whole series of java with the practical programs. I hope it will help you for learning java.

Java Introduction

Introduction of java :


Java is the object-oriented programming language. It is higher level programming language which is developed by Sun Microsystems and it is released in 1995. Java is run on the different-different platform like WINDOWS, LINUX, and UNIX.

Java depends on objects, classes, and functions. So in java its all about objects.

Object Oriented: Java is about objects so it can be extended easily since it depends on "Objects Models".

Simple: Java language is very easy to learn so if you know basic of java you can be the master of programming.

Platform Independent: Java is platform independent language. In java when the compiled independent means, file compiled and create the platform-independent byte file which is in (.class). This byte file also is known as bytecode which is interpreted by the virtual machine(JVM) and distributed over the web. And it is run on the different platforms.

 It does not depend on the specific machine like other languages C and C++.

Secure: Java is the secure language which has secure feature. It enables to develop the virus and tamper-free systems. It is authentication techniques are depended upon the public-key encryption.

Architectural-neutral: When compile the java file format and generate the architectural-neutral file, which compiled the file make the compiled code for executing different processor with java runtime system.

Portable: In java architectural-neutral and having no implementation aspects makes the Java portable.

Robust: In java mainly error comes while the compile time and runtime time checking, which makes the effort to eliminate error-prone situations.

Multithreaded: Multithreaded feature makes the easy task for the developer. The developer can write the program which performs many tasks simultaneously. It is used for construct interactive applications and it is running smoothly.

Interpreted: In java generate the bytecode and it is translated to the native machine instruction, and bytecode not stored anywhere.

High Performance: Java uses the Just-In-Time compiler, due to that it gives the high performance.

Distributed : Java is develop for distributed environment to internet.

Dynamic: Java language is dynamic than C and C++. In Java programs can carry the extensive amount of run-time. It can be used for resolve access and verify on run-time.

Software for Java :


First, download the Java JDK latest version from the official site. After installing the java. After the installed second most important thing is set the path for java.

Set the java path:


Step 1: Right-click on the computer and click on the property.

how to set path in java


Step 2: Then click on the Advanced system setting.

How to set path in java


Step 3: Click on the Environment Variable.

How to set path in java


Step 4: Click on the new or edit and set the path.

How to set path in java


The path for the window : c:\Program Files\java\jdk directory

Then we need Notepad or Notepad++ or other text editors.

Here is the example of java program :

java program


Output :

This my first Java program


Next: In next lecture, we will discuss the basic syntax of java.

Read More