Skip to main content

Java Variables and Types of variables in Java

 

Variable: 

The name of memory allocation which is used to store the data type during time execution.it is a combination of two words “vary or able” which means its value can be changed. 

Deceleration: 

We declare the variable as: Data_type Varible_Name; 

Initialization: 

We initialize the variable as: Varible_Name=value; 

Definition: 

We define the variable like as: Data_type Varible_Name = Value; Example: 

Int y; (Declaration of variable) 

y=10; (Initialization of variable) 

Int x=10; (Definition of variable) 

Types of Variables 

There are three types of variable are as follows: 

1. Local variable: programmingpoint

 A type of variable that is used inside the function and never accessed outside. 


2. Instance variable: 


 A type of variable that is declared inside the class and outside the function. It accesses the value through the object. 




3. Static variable: 


 A type of variable that is

declared with a static keyword throughout the program. 

Example to understand the types of variables in java 

Program:

class Variable { 

 static int x=23; //static variable 

 float instance=99; //instance variable 

 void local(){ //local variable 

 double a=34; 

 float b=45.5f; 

 double ab=a+b; 

 System.out.println("Sum of ab is: "+ab); System.out.println("The value of X: "+x); 

 public static void main(String[] args) { 

Variable obj=new Variable(); 

obj.local(); 

 System.out.println("Instance value: "+obj.instance);  } 

 } 

Output: 

 Sum of ab is: 79.5 

 The value of X: 23 

 Instance value: 99.0 





 How do we create the object in java? 

We create the object as:

Class_name object_Name = new Class_function();


Here we discuss some examples of variables in java. 

Add Two Numbers 



class Add { 

 public static void main(String[] args) { 

 float a=12.5f; 

 double b=44.6d; 

System.out.println("The sum of two numbers is: "+a+b); 

Output: 

 The sum of two numbers is: 12.544.6



Widening: 



 Widening is also known as upcasting. It is the conversion of greater primitive type value into smaller primitive type value. 



Example to understand the widening in java 

Program:


class Widening { 

 public static void main(String[] args) { 

 int x=23; 

 float a=x; 

 long b=x; 

 double c=x; 

 System.out.println("Float: "+a); 

 System.out.println("Long: "+b); 

 System.out.println("Double: "+c); 

Output: 

 Float: 23.0 

 Long: 23 

 Double: 23.0 

Narrowing(Typecasting): 


Narrowing is also known as downcasting. The conversion of narrowing is small primitive data type value into large primitive data type value. 


Example to understand the narrowing in java 


Program:


class Narrowing { 

 public static void main(String[] args) { 

 double a=23.5; 

float d=55.05f; 

long b=(long)a; 

double c=(double)a; 

int x=(int)a; 

float y=(float)a; 

byte z=(byte)d;

 System.out.println("Long: "+b);  System.out.println("Double: "+c);  System.out.println("Int: "+x); 

 System.out.println("Float: "+y);  System.out.println("Byte: "+z); 

} 

Output: 

 Long: 23 

 Double: 23.5 

 Int: 23 

 Float: 23.5 

 Byte: 55 


Overflow 


Program:


class Overflow { 

 public static void main(String[ ] args) { double db=150d; 

byte byt=(byte)db; 

System.out.println("byte"+byt); 

Output: 

 byte-106 

Underflow

Program:


class Underflow { 

 public static void main(String[ ] args) { double db=-200d; 

byte byt=(byte)db; 

System.out.println("Byte "+byt); 

Output: 

 Byte 56


Comments

Popular posts from this blog

Intorducation of data sturcture

Let’s start to introduction of data structure Data is an integral part of an application or program .It defines a program ,so program is nothing ,Set of instructions to perform on data to get some output.Then without data there are no instructions enough . no use of instructions , The term of data we use many palace like  Data sturcture  DataBases  Data Warehouse  Big Data  Let's start by introducing the data structure. Data Structure Data structure is the arrangement and collocation of data items . so that can be utilized and perform operations on data efficiently.Its all about arrangement and  efficient operation on data. That question is where inside the main memory during execution of the program.   So,without data structure there is no application.  Every Application will have a set of instructions to perform on data.So that data is mondetry ,where it will capture the data inside main memory. So, during the program execution how will the...

Text Widgets

  A widget displaying the string text with a single style is called text widget. import 'package:flutter/cupertino.dart' ; import "package:flutter/material.dart" ; void main () { runApp( MyApp ()) ; } class MyApp extends StatelessWidget { @override Widget build (BuildContext context) { // TODO: implement build return MaterialApp ( home: Scaffold ( backgroundColor: Colors. teal , body: SafeArea ( child: Column ( children: <Widget>[ CircleAvatar ( radius: 50 , backgroundImage: NetworkImage ( 'https://dsearch.com/images?q=avatar%20user%20png' ) , ) , Text( 'Ahmad Mukhtiar', style: TextStyle( fontSize: 40.0, color: Colors. white , fontWeight: FontWeight. bold , ) , ), ] , ...