1

Primitive Data Types In Java

Definition :

- A Data Type is the characteristic of a variable that determines what kind of data it can hold.

Java comes with eight primitive data types to handle simple data values. A primitive type is predefined by the language and is named by a reserved keyword. The eight primitive data types supported by the Java programming language are listed below.



boolean :

The boolean data type has only two possible values: true and false. We use this data type for conditional statements. true and false are not the same as True and False. Boolean may not be cast into any other type of variable nor may any other variable be cast into a boolean.

Example :  boolean a = true ;

byte :

The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. It has a minimum value of -128 and a maximum value of 127.

Example :  byte a = 200 ;

short :

The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767.Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int.

Example :  short a = 20000 ;

int :

The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. Int is generally used as the default data type for integral values unless there is a concern about memory. The default value is 0.

Example :  int a = 500 ;

long :

The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807.
This type is used when a wider range than int is needed. Default value is 0L.

Example :  long a = 100000L ;

float :

Float data type is a single-precision 32-bit IEEE 754 floating point. Float is mainly used to save memory in large arrays of floating point numbers. Default value is 0.0f.

Example :  float a = 132.5f ;

double :

The double data type is a double-precision 64-bit IEEE 754 floating point. This data type is generally used as the default data type for decimal values. Double data type should never be used for precise values such as currency. Default value is 0.0d.

Example :  double a = 123.4 ;

char :

The char data type is a single 16-bit Unicode character. Char data type is used to store any character.It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Example :  char a = 'b' ;

This are the eight primitive data types in java and you can know about access specifiers which are used with this data types from Access Specifiers In Java.
More
0

Keywords In Java

Definition :

- keywords are reserved words in Java Programming which have a predefined meaning in the language and so we cannot use these keywords as variables, methods, classes, or as any other identifier in the program.



The above image shows list of all keywords used in java programming. Will learn in detail on those keywords later tutorials.
More
0

Comments In Java

Definition :

- Java comments are either explanations of the source code or descriptions of classes, interfaces, methods, and fields.They are usually a couple of lines written above or beside Java code to clarify what it does.

Java has three kinds of comments and they are traditional comments, end-of-line comments and documentation comments. Lets see in detail about it.

Traditional comments :

Traditional comments start with /* and end with */, they may span across multiple lines.Everything in between these two delimiters is ignored by the Java compiler. This type of comments was derived from C and C++.

Example :

class Demo {
public static void main(String[] args) {
System.out.println("Hello World!"); /* www.java-answers.blogspot.com */
}
}

End-of-line comments :

End-of-line comments start with // and extend to the end of the current line. The compiler ignores everything from // to the end of the line.

Example :

class Demo {
public static void main(String[] args) {
System.out.println("Hello World!"); //www.java-answers.blogspot.com
}
}

Documentation comments :

Documentation comments are processed by Javadoc tool to generate documentation from source files. Documentation comment is readable to both, computer and human and to start the comment, use /** instead of and end with */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.

Example :

/**
* Java Answers is a place to get all Java resource
* visit www.java-answers.blogspot.com
*/
class Demo {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

This is how comments are created in java and will learn more about this in next tutorial on java comments.
More
53

Access Specifiers In Java

Definition :

- Java Access Specifiers (also known as Visibility Specifiers ) regulate access to classes, fields and methods in Java.These Specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class. Access Specifiers can be used to restrict access. Access Specifiers are an integral part of object-oriented programming.

Types Of Access Specifiers :

In java we have four Access Specifiers and they are listed below.

1. public
2. private
3. protected
4. default(no specifier)

We look at these Access Specifiers in more detail.



public specifiers :

Public Specifiers achieves the highest level of accessibility. Classes, methods, and fields declared as public can be accessed from any class in the Java program, whether these classes are in the same package or in another package.

Example :

public class Demo { // public class
public x, y, size; // public instance variables
}

private specifiers :

Private Specifiers achieves the lowest level of accessibility.private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses. So, the private access specifier is opposite to the public access specifier. Using Private Specifier we can achieve encapsulation and hide data from the outside world.

Example :

public class Demo { // public class
private double x, y; // private (encapsulated) instance variables

public set(int x, int y) { // setting values of private fields
this.x = x;
this.y = y;
}

public get() { // setting values of private fields
return Point(x, y);
}
}

protected specifiers :

Methods and fields declared as protected can only be accessed by the subclasses in other package or any class within the package of the protected members' class. The protected access specifier cannot be applied to class and interfaces.

default(no specifier):

When you don't set access specifier for the element, it will follow the default accessibility level. There is no default specifier keyword. Classes, variables, and methods can be default accessed.Using default specifier we can access class, method, or field which belongs to same package,but not from outside this package.

Example :

class Demo
{
int i; (Default)
}

Real Time Example :



This are the Access Specifier in java and we will learn in detail about these Access Specifier in future chapters.
More
1

What Are Object And How To Create Object In Java ?

Definition :

- Objects are the basic run time entity or in other words object is a instance of a class. All these objects have a state and behavior.If we consider the real-world we can find many objects around us, Cars, Dogs, Humans etc. If we consider a dog then its state is name, breed, color, and the behavior is barking, wagging, running.Software objects also have a state and behavior.

Creating an Object :

In java the new keyword is used to create new objects.There are three steps when creating an object from a class.

Declaration : A variable declaration with a variable name with an object type.

Instantiation : The 'new' keyword is used to create the object.

Initialization : The 'new' keyword is followed by a call o a constructor. This call initializes the new object.

Syntax for creating Object :

class_name object_name = new class_name();

Example :

class demo
{
int i=10;
void display()
{
System.out.println("www.java-answers.blogspot.com");
}
public static void main(String args[])
{
demo x = new demo();
x.display();
System.out.println("value of i = " +i);
}
}

Output :
www.java-answers.blogspot.com
value of i = 10

Accessing Methods Using Object :

Instance variables and methods are accessed via created objects.you can learn about Methods and how to create Methods from what are methods in java tutorial. To access a Method from the above example should be as follows:

/* First create an object */
demo x = new demo();

/* Now you can call a class method as follows */
x.display();

The above example program creates a object with name x using 'new' keyword and after creating the object we can access the method and variable of class using this objects as shown in the above program.This is how objects are created in java.
More
0

What Is Java Methods ?

Definition :

- A Java method is a collection of statements that are grouped together to perform an operation.

- A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name.

Creating a Method :

Each method has its own name. When that name is encountered in a program, the execution of the program branches to the body of that method. When the method is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code.

Method Syntax :

modifier returnValueType methodName(list of parameters)
{
// Method body;
}

Modifiers : The modifier, which is optional, tells the compiler how to call the method. This defines the access type of the method.

Return Type : A method may return a value. The returnValueType is the data type of the value the method returns. Some methods perform the desired operations without returning a value. In this case, the returnValueType is the keyword void.

Method Name : This is the actual name of the method. The method name and the parameter list together constitute the method signature.

Parameters : A parameter is like a placeholder. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.

Method Body : The method body contains a collection of statements that define what the method does.

Example :
// Simple Method Example
public void test()
{
System.out.println("this is a simple method example");
}

This is how Methods are created in java and you can know how to access Methods using Objects from what are objects and how to create object in java . we will learn different types of methods in the later tutorials of java methods.
More
0

What Is A Java Class ?

Definition :

- Java Class is nothing but a template for object you are going to create (or) its a blue print by using this we create an object.

- In simple words we can say its a pattern which we define and every object we define will follow the pattern. class are fundamental building.

What does Java Class Consist :

  • When you create class in java the first step is keyword class and then name of the class or identifier we can say.

  • Next is class body which starts with curly braces { } and between this all things related with that class means their property and method will come here.

Class Syntax :

<access specifier> class <identifier>
{
<member>
...
}

Access Specifiers Of Class :

Java class has mainly two type of access level and they are listed below.

Default: class objects are accessible only inside the package.

Public: class objects are accessible in code in any package.

Members of Class :

The contents of the body of a class consists of a collection of <members>. Members consist of fields and methods.

Field : Things in the Java language which hold data, or hold pointers to objects are called fields. Think of fields as variables.

Method : Method is a set of statement that perform a particular task. It is similar to function is c/c++.

This are basic's of Class in java and will look in detail about Class in next tutorial.
More
0

Java 2 Complete Reference, Fifth Edition



This book is the most complete and up to date resource on Java from programming guru, Herb Schildt. Its the best book for beginners and who wants to learn entire concepts of core java. A must have desk reference for every Java programmer. This comprehensive resource contains everything you need to develop, compile, debug, and run Java applications and applets.


Download
More
10

OOPS And Its Concepts Or Properties In Java

Definition :

OOPS stands for Object Oriented Programming.Object Oriented Programming is the technique to create programs based on the real world. Unlike procedural programming, here in the OOP programming model programs are organized around objects and data rather than actions and logic.

OOPS Concept :

OOPS reduce the code of the program because of the extensive Properties.OOPS
Properties or Concepts are mainly 4 and they are listed below.

1.Encapsulation
2.Abstraction
3.Inheritance
4.Polymorphisam

lets see in detail about the concepts of OOPS.

1. Encapsulation :

It is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper. Idea of encapsulation is to hide how a class does it but to allow requesting what to do.

Example : Medical Capsuals

i.e one drug is stored in Bottom layer and another drug is stored in Upper layer these two layers are combined in single capsual.

2. Abstraction :



Hiding non-essential features and showing the essential features

                                          (or)

Hiding unnecessary data from the users details,is called abstraction.

Example: TV Remote Button

i.e In TV Remote there are numbers, power and other buttons. We can just see those buttons,we don't see the button circuits and wirings.so non-essential features are hidden from the user.

3. Inheritance :




creating a new class from existing class is called Inheritance.reusability is main advantage in inheritance.The new classes, known as derived classes take over (or
inherit) attribute and behavior of the pre-existing classes,which are referred to as base classes (or Parent classes).

Example : Father and Son Relationship

4.Polymorphisam :




The ability to take more than one form. The abiltiy to define more than one function with the same name is called Polymorphism. In java there are two type of polymorphism.

a) compile time polymorphism (overloading)
b) runtime polymorphism (overriding).

This are the 4 main Properties or Concepts of OOPS. You can leave a comment if you have doubt in it.
More
0

Simple Hello World Java Program

This is a very simple Java program for beginners.To create a Hello World Program we need to write codes which are shown below in notepad and save it with the name HelloWorld.java. It must be sure that the file name must match the name of the class.You have to pay attention that this naming convention is CASE SENSITIVE it means that naming your file into helloworld.java WON'T work.


class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}


How to Compile and Run Your Program :

javac HelloWorld.java command is used to compile the source code. A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer(byte-codes. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version).

Once you compile your program, you can run it.

java HelloWorld command can execute the byte code in the Java interpreter.Notice that there is no .class extension on it.

Output :




Understanding the program :

- class HelloWorld

Class is the basic building block of the java program, java codes are written in the java class.Here HelloWorld is the name of Class and you can Use Any name.

- public static void main (String args[])

This is necessary to for any class to be runnable. This function is the entry point of the execution.

public - This access modifier makes this method accessible everywhere.
static - It will make this main method invokable without creating an instance of this class.
void - void declares that the method does not return any value.
main - it is a thread which the JVM looks first and is the entry point for the program
String args[] - declares a parameter named args, which is an array of instances of the class String.

- System.out.println("Hello World!");

This command is used to print some text on the screen.The text has to be surrounded by the double quotes. In this case, "Hello World!" is being printed. Remember that almost every command in Java ends with a semicolon(;).
More
0

Tutorial To Set Path For Java In Windows

This Tutorial is about how to set a path for java in windows operating system. You need to install java first to set a path. You can get details of how to install java from Tutorial To Download And Install Java In Windows . Now After installing java go to C Drive -> Program Files -> Java and follow the below steps
















When you click on New then you will get option asking Variable name and Variable value in it. You can write any Variable name but you need to paste the path which you have copied earlier in to Variable value and click OK as shown in below image.





After clicking New in top of window you will need to write Variable name and Variable value again as shown in the below image.





clicking on OK Java path will be successfully set on your windows and hope this tutorial will help you all and also visit Video Tutorial To Set Path For Java In Windows Operating System to see the video tutorial of it. If you have any doubts in it you can leave a comment or contact me through email.
More
0

Tutorial To Download And Install Java In Windows

This Tutorial is about how to download and install java in windows operating system. Following the below image tutorial you will successfully install java. Hope this tutorial will help you and if any doubts in it you can leave a comment.

More
1

Introduction To Java

History :

Java is a programming language created by James Gosling from Sun Microsystems(which is now a subsidiary of Oracle Corporation) in 1991.The language was initially called Oak after an oak tree that stood outside Gosling's office, and was later renamed Java, from a list of random words.The first public available version of Java (Java 1.0) was released 1995. Over time several version of Java were released which enhanced the language and its libraries. The current version of Java is Java 1.6 also known as Java 6.0.

Overview :

1. The Java programming language consists out of a Java compiler, the Java virtual machine, and the Java class libraries. The Java virtual machine (JVM) is a software implementation of a computer that executes programs like a real machine.
2. The Java compiler translates Java coding into so-called byte-code. The Java virtual machine interprets this byte-code and runs the program.
3. The Java virtual machine is written specifically for a specific operating system.
4. The Java runtime environment (JRE) consists of the JVM and the Java class libraries.
5. Java is an object-oriented programming language and much of the syntax of Java is the same as C and C++. One major difference is that Java does not have pointers.
More