JAVA BASICS

Hello Java Program:


Program Compilation:


Run the Program:


Variable Types in Java:

Syntax:
data type variable [ = value] [, variable [ = value] ... ];

  • Local Variables
  • Instance Variables
  • Class/Static Variables

Local Variables

Instance Variables

Class/Static Variables

These are declared in methods, constructors, or blocks. These are declared in a class, but outside a method, constructor or any block. These are also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
Created when the method, constructor or block is entered and the variable will be destroyed once it exists the method, constructor, or block. When a space is allocated for an object in the heap, a slot for each instance variable value is created. There would only be one copy of each class variable per class, regardless of how many objects are created from it.
Access modifiers cannot be used for local variables. These are created when an object is created with the use of the keyword "new" and destroyed when the object is destroyed. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.
These are visible only within the declared method, constructor, or block. These hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class. Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants.
These are implemented at stack level internally. These can be declared in class level before or after use. Static variables are created when the program starts and destroyed when the program stops.
There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use. Access modifiers can be given for instance variables. Static is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.

Local variable Example:



Instance Variable Example:



Class/Static Variable Example:




Java - Data Types:

Type

Description

Default

Size

Example - Literals

Boolean True or False False 1 bit true, false
Byte Twos complement integer 0 8 bits (none)
Char Unicode character \u0000 16 bits 'a','\u0041','\101','\\','\',',','\n'
Short Twos complement integer 0 16 bits (none)
Int Twos complement integer 0 32 bits -2,-1,0,1,2
Long Twos complement integer 0 64 bits -2L, -1L, 0L, 1L, 2L
Float IEEE 754 floating point 0.0 32 bits 1.23e100f, -1.23e-100f, .3f, 3.14F
Double IEEE 754 floating point 0.0 64 bits 1.23456e300d, -1.23456e-300d, 1e1d
Primitive Data Types

Primitive Data Types Example:




Java - Strings:

  • String is a special type of class in Java.
  • Strings are widely used in Java programming, are a sequence of characters.
  • In Java programming language, strings are treated as objects.

String Creation Example:



Java- Loop control:



While Loop:


For Loop:

do... While Loop:


All Loop Control in one Example:



Read from keyboard and write to screen:



Read From File:

First, create one text file named as ramesh.txt and save in particular location



Now write a program as



Write to a file



Reading Second and Fourth Line from the file:




Number of occurrences of a String in a file:



Number of occurrences of a String and replace in a file:

Before replacement the text file as




Graphics Output:

Drawing a Rectangle:



Drawing a Line:



Drawing a Circle:



Drawing a Star:



Number count in  a String:



Java Database Connectivity to SQL Server:



Getting Records from the database:



Inserting a record to the database:




First Non Repeated Character in a String:



Permutation of a String:



Finding Numbers in a String:



Replacing the first highest repeated character with the user given character:



Program to print the following pattern
          1
        121
      12321
    1234321
  123454321
12345654321
  123454321
    1234321
      12321
        121
          1




Program to print sum of numbers in a given string:




Previous
« Prev Post