• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Array 's == Ques

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:
int i[],j[];
int k[][], m[][];
Which one will print true
A) i == j
B) i == k
C) k == m
D) m == j
I think A, C will print true. What do you guy think?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think none of them will print true since i,j,k and m are all different variables and no assignment are made... == is a reference equality operator. Be aware that array are object even arrays of primitives !!! so comparing different array references like i==j will return false if the assignment i=j or j=i has not been made before the comparison ...
Hope it helps
Val
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It wont return false either as it is a compile time error. All the Arrays are un-intialized and they are being accessed.
Correct me if I'm wrong.
Thanx.
Jennifer.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jennifer is correct. It would give a compile time error. Something like, "variable i may not have been initialized."
Brian
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right Jennifer, the output is

Val
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
I think none of them will print true since i,j,k and m are all different variables and no assignment are made... == is a reference equality operator. Be aware that array are object even arrays of primitives !!! so comparing different array references like i==j will return false if the assignment i=j or j=i has not been made before the comparison ...
Hope it helps
Val



Val,
Arrays are objects in java
So i,j,k,m basically becomes Object references perse'
if you declare them as instance variable
they all get declared and initialized as null
So i==j , j==i and k==m
will certainly work and results as Equal
But i==k wont even compile becoz
i is single dimension , k is multi dimension
Ragu

[This message has been edited by Ragu Sivaraman (edited September 14, 2001).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the code
class equal {
static int k[][], m[][];
public static void main (String args[]) {
System.out.println(k == m);
}
}
Result : true
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Anil
If they are fields they get initialized to null.
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic