The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Variables and Types of Variables

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java Variables and Types of Variables Posted: Apr 14, 2015 7:29 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Variables and Types of Variables
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

Variable:

  • Variable is a named memory location used to store data temporarily.
  • During program execution we can modify that data

Limitation of Variable:

  •  It can only store single value at a time.
  • It means , always it returns latest modified value.

How can a variable be created?

  • A variable can be created by using data type.
  • As we have two types of data types  we can create two types of variables.
    1.Primitive Variable
    2.Referenced variable
  • The difference between primitive and referenced variable is "primitive variable stores data directly , where referenced variables stores reference/address of object, not direct values"

  1. package com.instanceofjava;
  2.  
  3. class Example
  4. {
  5.  
  6.  int x=10;
  7.  int y=20;
  8.  
  9. }


  1. package instanceofjava;
  2. class Test
  3. {
  4.  
  5. public int get(){
  6. return 10:
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11. //primitive variables
  12. int s=50;
  13. int t=get(); 
  14.  
  15. //reference variables
  16. String str="a";
  17. String str1=new String("a");
  18. Example e= new Example();

  19. }
  20. }



What is an object?

  • Technically object means collection of all non static variables and methods memory locations.
  • Object is created using new keyword

Defining a variable:

  • Variable creation with value is called defining a variable
  • <Accessibility modifier><Modifier><Data type><Variable name>=<Value> ;

  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. public static int x=10;
  6. public static Example e= new Example();

  7. }
  8. }

Declaring a variable:

  • Variable creation without value is called declaring a variable.
  • <Accessibility modifier><Modifier><Data type><Variable name>;


  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. public static int a;
  6. public static Example e;

  7. }
  8. }


Initializing/Assigning a variable:

  • Storing a value in a variable at the time of its creation is called initializing a variable.
  • Storing a value in a variable after its creation is called assigning a value to a variable.
  • <Variable_Name>=<Value>;

  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. //declaring a variable
  6.  int a;
  7.  int x;
  8.  
  9. //assigning a variable
  10.  a=20;
  11.  x=10;
  12.  
  13. // re initialization/ re assignment
  14.  a=30;
  15.  x=20;

  16. }
  17. }


Calling a variable:

  • Reading a value from a variable is called calling a variable.


  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5.  int x=10;
  6.   
  7.  //calling x variable for printing
  8. System.out.println(x);
  9.  
  10. // calling x variable for initializing y
  11.  int y=x;


  12. }
  13. }

Types of Variables:

  • Local
  • Static
  • Non static
  • Final
  • Transient
  • Volatile


Read: Java Variables and Types of Variables

Topic: Java : Collection Framework : Hashing and HashCode Previous Topic   Next Topic Topic: Java : Collection Framework : When to use ArrayList and Why?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use