Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Monday, 9 March 2015

Array in Java

 Unknown     09:47     Java     No comments   

An array is a group of variables of same types containing some values.In Java Arrays are objects so they are considered reference types.The elements of array can be primitive or reference types.If we want to access any element in the array, then we use the position number of that element in the array.This position number in the array is called the index or subscript of the element.
Declaration of array:
datatype[] arrayReference;   //preferred way
or
datatype arrayReference[];

For ex:   int[] arrayOfInts;
is an array which is pointing to the elements of type integer.
Defining array:
arrayReference=new datatype[arraySize];

For ex: arrayOfInts=new int[10];

We can combine the above two steps in one step as
 int[] arrayOfInts=new int[10];

So in this way we can define an array.
If the size of array is 10 then its index would be 0-9 i.e. 0 to (size-1).
For ex int[] arr=new int[5];
then its elements would be arr[0],arr[1],arr[2],arr[3],arr[4] and the array reference will be pointing to the first element that is arr[0].
The index of array can be a expression but it can't be negative.
For ex arr[a+b]=23;

How to pass an array to method?
Suppose we have an array int[] arr=new int[10];
and method declaration getArrayMethod(int[] a);
then the method call        getArrayMethod(arr);
passes the reference of array arr to method getArrayMethod().
The argument to a method can be passed as an entire array or only a single element.When a argument to the method is entire array, then it receives a copy of reference and when argument to method is an individual array element of primitive type then the method receives the value of that element.
Pass by value and pass by reference:
There are two ways to pass the arguments to the method:
1)Pass by value
2)Pass by reference

When an argument is passed by value,a copy of argument's value is passed to the called method.So if we made any changes to that argument then that changes will only be valid inside that called function since method contains the copy of that argument.So that updated value will not affect the original value of that argument outside that method.
If argument is passed by reference then called method can access the argument's value in caller directly and can modify its value.Pass by reference is used to improves the performance because in this case we don't have any need to copy the data,we only have to copy the reference of that data.
In Java,we cannot choose to pass by value or pass by reference, all arguments are passed by value.If argument is primitive type then copy of that primitive value is passed and if argument is reference type then copy of that reference is passed.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ▼  March (20)
      • ArrayList Methods in Java
      • ArrayList Methods in Java
      • ArrayList in Java
      • Arrays Class in Java
      • Java Interview Question-Variable length arguments ...
      • Array in Java
      • Data Hierarchy in Operating Systems
      • Synchronization in Java
      • Typecasting in Java
      • Concurrency in Java
      • Runnable in Java
      • HashMap in Java
      • Reverse of a given number using Java
      • Star Pattern printing in Java
      • Difference between fail-fast and fail-safe iterators
      • Factorial of a number in Java
      • Null in Java
      • Final in Java
      • Immutable Objects in Java
      • Thread concept in java
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates