What is java primitive data types

Data types are used to represent type of the variable & expressions.
Representing how much memory is allocated for variable.
Specifies range value of the variable.

There are 8 primitive data types in java
Data Typesize(in bytes)Rangedefault values
byte1 -128 to 1270
short 2-327680
int 4-2147483648  to 2147483647 0
long89,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807 
float 4-3.4e38 to 3.4e 0.0
double 8-1.7e308 to 1.7e308 0.0
char 20 to  6553single space
Boolean no-size no-rangefalse

What are java identifiers

Every name in java is called identifier such as,

  • Class-name
  • Method-name
  • Variable-name

Rules to declare identifier:

  1. An identifier contains group of Uppercase & lower case characters, numbers ,underscore & dollar sign characters but not start with number.

int abc=10;      —> valid                          int _abc=30;  —> valid              int $abc=40;  —>valid

int a-bc=50;    —>not valid                   int 2abc=20;  —> Invalid           int not/ok=100 -invalid

2.Java identifiers are case sensitive of course java is case sensitive programming language. The below three declarations are different & valid.

class Test

{

int NUMBER=10;

 int number=10;

int Number=10;

}

3.The identifier should not duplicated & below example is invalid because it contains duplicate variable name.

class Test{

int a=4;

int a=5;

}

4.In the java applications it is possible to declare all the predefined class names & interfaces names as a identifier but it is not recommended to use.
class Test
{
public static void main(String[] args)
{
int String=10;

float Exception=10.2f;
System.out.println(String);
System.out.println(Exception);

}

5.It is not possible to use keywords as a identifiers.

class Test
{
int if=10;
int try=20;
}

6.There is no length limit for identifiers but is never recommended to take lengthy names because it reduces readability of the code.

 

}

Difference between println() and print() methods in java

Println():- used to print the statements in console but the control is there in next line.

Example:-

System.out.println(“Sravyainfotech”);

System.out.println(“core java”);

Output: –             Sravyainfotech

Core java

Print():- used to print the statement in console and the control is present in the same line.

Example:-

System.out.print(“Sravyainfotech”);

System.out.print(“core java”);

Output:-SravyaInfotechcorejava

Separators in java

Symbolnameusage
( )parenthesesused to contains list of parameters & contains expression.
{ }bracesblock of code for class, method, constructors & local scopes.
[ ]bracketsused for array declaration.
;semicolonterminates statements.
,commaseparate the variables declaration & chain statements in for.
.periodused to separate package names from sub packages. And also
   used for separate a variable,method from a reference type.

How to write comments in java

  • Comments are used to write the detailed description about application logics to understand the logics easily.
  • Comments are very important in real time because today we are developing the application but that application maintained by some other person so to understand the logics by everyone writes the comments.
  • Comments are non-executable code these are ignored at compile time.

There are 3 types of comments.

Single line Comments:-

By using single line comments it is possible to write the description about our programming logics within a single line & these comments are Starts with // (double slash) symbol.

Syntax:-       //description

Multi line Comments:-

This comment is used to provide description about our program in more than one line & these commands are start with /* & ends with */

Syntax: –       /*—-satement-1

—-statement-2

*/

Documentation Comments:-

By using documentation comments it possible to prepare API(Application programming interface) documents.(e will discuss later chapte)

Syntax: – 

   /*

*statement-1

*statement-2

*/

Steps to Design first java application

Step-1:- Select an Editior.

Step-2:-Write the application.

Step-3:-Save the application.

Step-4:-Compilation Process.

Step-5:-Execution Process.

Step1:- Select an Editor

Editor is a tool or software it will provide very good environment to develop java application. Ex :- Notepad, Notepad++,edit Plus…..etc

IDE:- ( Integrated development Environment )

IDE is providing very good environment to develop the application.

Ex:- Eclipse,MyEclipse,Netbeans,JDeveloper….etc

IDE is a real-time standard but don’t use IDE to develop core java applications because 75% work is done by IDE & remaining 25 % work is down by developer.

75% work of IDE is:-

  1. Automatic compilation.
  1. Automatic package import.
  1. It shows all the predefined methods of classes.
  1. Automatically generate try catch blocks and throws (Exception handling)
  1. It is showing the information about how to fix the bug………………………etc

Note :- Do the practical’s of core java only by using Edit Plus software.

Step 2:-  Write a program.

Example application:-

import java.lang.System;

import java.lang.String;

class Test             //class declaration

{//class starts

public static void main(String[] args)      //program execution starting point

{//main starts

System.out.println(“hi Atul”); //printing statement

}//main ends

}; //class ends class A

{

}

class B

{

}

In above example String & System classes are present predefined java.lang package hence must import that package by using import statement.

There are two approaches to import the classes in java, 1) Import all class of particular package.

a.    Import java.lang.*;    //it is importing all classes of java.lang package.

  1. Import required classes
    1. Import java.lang.System;
    1. Import java.lang.String;

In above two approaches second approach is best approach because we are importing application required classes.

Note: The source file is allows declaring multiple java classes.

Step3:- save the application.

  1. After writing the application must save the application by using (.java) extension.
  • While saving the application must fallow two rules:

If the source file contains public class then public class name & Source filename must be same (publicClassName.java). Otherwise compiler generate error message.

if the source file does not contain public class then save the source file with any name (anyName.java) like A.java , Ratan.java, Anu.java …….etc .

Note: – The source file allowed only one public class, if we are trying to declare multiple public classes then compiler generate error message.

Step-4:- Compilation process.

Compile the java application by using javac command.

Syntax:-               Javac   filename

Javac Test.java

Process of moving application saving location:-

C:\Users\hp> initial cursor location

C:\Users\hp>d: move to local disk D

D:\>cd atul changing directory to ratan

D:\atul>javac Sravya.java compilation process

Whenever we are performing compilation the compiler will check the syntax errors.

If the application contains syntax errors then compiler will generate error message in the form of compilation error.

If the application does not contains syntax errors then compiler will generate .class files.(conversion of .java to .class)

Note: – in java .class files generated by compiler at compilation time and .class file generation based on number of classes present in source file.

If the source file contains 100 classes after compilation compiler generates 100 .class files

The compiler generate .class file and .class file contains byte code instructions it is intermediate code.

Process of compiling multiple files:-
D:  
|–>atul
 |–>Sravya.java
 |–>A.java
 |–>B.java
 |–>C.java
javacA.javaone file is compiled(A.java)
javacB.java C.javatwo files are compiled
javac *.javaall files are compiled

Step-5:- Execution process.

Run /execute the java application by using java command.

Syntax:-               Java  class-name

Java  Test

Whenever you are executing particular class file then JVM perform fallowing actions.

JVM wills loads corresponding .class file byte code into memory.

After loading .class file JVM calls main method to start the execution process.

In above two cases if the class file or main method is not available then at runtime JVM will generate error message.

If the main method is not available: “Main method not found in class A, please define the main method”.If the .class is not available : “Could not find main class”.

Executing all generated .class files based on example given in second step:-

Test class—>class is loaded & main is present
A class—>class is loaded but main is not present
B class—>class is loaded but main is not present
XXX class—>XXX class is not present

D:\atul>java Test

Hi Atul

D:\atul>java A

Error: Main method not found in class A, please define the main method as:

D:\atul>java B

Error: Main method not found in class B, please define the main method as:

D:\atul>java XXXError: Could not find or load main class XXX

1.compiler is translator it is translating .java it is translating .class file to machine code.file to .class where as JVM is also a translator

2.Compiler understandable file format is .java file but JVM understandable file format is .class file.

3.It is possible to compile multiple files at a time but it is possible to execute only one .class file at a time.

4.The .java file contains high level language (English) but .class file contains byte code instructions.

5.java is a platform independent language but JVM is platform dependent.

Conclusion 1:- Java contains 14 predefined packages but the default package in java is java.lang

class Test

{

public static void main(String[] args)

{

System.out.println(“hi ratan”);

}

}

Conclusion -2:-

The class contains main method is called Main class and java allows to declare multiple main

class in a single source file.

Conclusion -3:The source file is allows to declare only one public class, if you are declaring morethan

one public class compiler generate error message.

conclusion-4 The below example compiled & executed but it is not recammanded because the class

name starting with lower case letters.


Java coding convention

Classes:-

  • Class name start with upper case letter and every inner word starts with upper case letter.
  • This convention is also known as camel case convention.
  • The class name should be nouns.

Ex:-      String                 StringBuffer                 InputStreamReader    ……etc

Interfaces :-

  1. Interface name starts with upper case and every inner word starts with upper case letter.
  • This convention is also known as camel case convention.
  • The class name should be nouns.

Ex: Serializable        Cloneable           RandomAccess

Methods :-

  • Method name starts with lower case letter and every inner word starts with upper case letter.
  • This convention is also known as mixed case convention
  • Method name should be verbs.

Ex:- post()           charAt()                toUpperCase()                   compareToIgnoreCase()

Variables:-

  • Variable name starts with lower case letter and every inner word starts with upper case letter.
  • This convention is also known as mixed case convention.

Ex :- out     in    pageContext

Package :-Package name is always must written in lower case letters. Ex :-java.lang java.util java.io …etc

Constants:-

  • While declaring constants all the words are uppercase letters .

Ex: MAX_PRIORITY        MIN_PRIORITY         NORM_PRIORITY

NOTE:- The coding standards are mandatory for predefined library & optional for user defined library but as a java developer it is recommended to fallow the coding standards for user defined library also.

How to set path in java

Install the software and set the path :-

  • Download the software.
  • Install the java software in your machine.
  • Set the environmental variable.

Download the software:-

ü Download the software from internet based on your operating system & processor

because the software is different from operating system to operating system & processor to processor.

Install the java software in your machine:-

Local Disk c: —>program Files—>java —>jdk(java development kit),jre(java runtime nvironment)

After installing To check whether the java is installed in your system or not open the command prompt type javac command.

Process to open command prompt: Start —>run—->open:cmd—->ok

C:\Users\ATUL>javac

‘javacis not recognized as an internal or external command ,operable program or batch file.”

Whenever we are getting above information then decide in our system java is installed but the java is not working.

Why java is not working Reason:-

C:\Users\ATUL>javac

Whenever we are typing javac command on the command prompt operating system will pickup javac command search for that command,

  1. in the internal operating system calls but javac is not available in the internal system calls list.
  1. If it not available in internal system calls list then immediately it won’t raise any error, it will search in environmental variables

In above two cases if the javac command is not available then operating system will raise error message “javac is not recognized as an internal or external command”To overcome above problem to make eligible javac command operating system set environmental variables.

The location of javac command is : C:\Program Files\Java\jdk1.7.0\bin

Right click on mycomputer—>properties—–>Advanced system setting—>Environment Variables —
User variables  >new   >——-variable name : path
 Variable value :  C:\programfiles\java\jdk1.6.0_11\bin;
 —–>ok—->ok

Now the java is working in your system to check open the new command prompt & type javac command then we will get list of commands then decide in your system java is working.

In your system or your friend system to check java is installed or not open the command prompt

  • type javac command
    • If error message displayed java is not working.(‘javac’ is not recognized as an internal or external command)
    • If list of commands are displayed then decide java is working properly.

Now the java is working in your system to check open the new command prompt & type javac command then we will get list of commands then decide in your system java is working.

In your system or your friend system to check java is installed or not open the command prompt

  • type javac command
    • If error message displayed java is not working.(‘javac’ is not recognized as an internal or external command)
    • If list of commands are displayed then decide java is working properly.
Design a site like this with WordPress.com
Get started