473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

arrays in JAVA

Hello Everyone..
The following code module is giving 13 as the answer in JAVA
class Number
{
public static void main(string args[])
{
int arr[] = {4,8,16};
int i = 1;
arr[++i] = --i;
System.out.println(arr[0]+arr[1]+arr[2]);
}

I m getting the same output n c# as well...

But the same code in c and c++ is giving the output as 21.It is as below...

#include <stdio.h>
void main()
{
int arr[] = {4,8,16};
int i = 1;
arr[++i] = --i;
printf("%d",arr[0]+arr[1]+arr[2]);
}

How can the same program give different outputs in diffrent languages when the concept of array remains the same inall languages?
Can anyone tell me please?
Nov 2 '07 #1
4 1388
JosAH
11,448 Expert 8TB
Java evaluates expressions strictly from left to right, so first (on the left of the
assignment operator) i will be equal to 2 and on the right side of that same operator
the value will be one (1) again, effectively assigning a[2] = 1. 4+8+1 == 13.

In C and C++ this expression causes 'undefined behaviour' by definition of the ISO
Standard that is, so nothing can be said about the value of the expression, no
matter that your implementations returned 21. Even daemons could fly out of
your nose because of the undefined behaviour.

C# was simply stolen from Sun's Java so I'm rather confident that it'll return 13 too.

kind regards,

Jos
Nov 2 '07 #2
r035198x
13,262 8TB
Hello Everyone..
The following code module is giving 13 as the answer in JAVA
class Number
{
public static void main(string args[])
{
int arr[] = {4,8,16};
int i = 1;
arr[++i] = --i;
System.out.println(arr[0]+arr[1]+arr[2]);
}

I m getting the same output n c# as well...

But the same code in c and c++ is giving the output as 21.It is as below...

#include <stdio.h>
void main()
{
int arr[] = {4,8,16};
int i = 1;
arr[++i] = --i;
printf("%d",arr[0]+arr[1]+arr[2]);
}

How can the same program give different outputs in diffrent languages when the concept of array remains the same inall languages?
Can anyone tell me please?
Yep, C# would give 13 for the reasons given by Jos above.

P.S In Java you want to have the argument to main as String args[] not string args[]
Nov 3 '07 #3
Java evaluates expressions strictly from left to right, so first (on the left of the
assignment operator) i will be equal to 2 and on the right side of that same operator
the value will be one (1) again, effectively assigning a[2] = 1. 4+8+1 == 13.

In C and C++ this expression causes 'undefined behaviour' by definition of the ISO
Standard that is, so nothing can be said about the value of the expression, no
matter that your implementations returned 21. Even daemons could fly out of
your nose because of the undefined behaviour.

C# was simply stolen from Sun's Java so I'm rather confident that it'll return 13 too.

kind regards,

Jos


Thanks for the reply..
I have a small doubt..Since we are using the same 'i' as the index to access the array elements,shouldn't reflect back on the left hand side when the decrement operation is done on the right hand side..i.e we are using 'i' as the index to access the array elemts and 'i' becomes 2 first on the right side and the same 'i' becomes 1 on the left hand side ,so effectively i now has 1 and arr[i] is now arr[1] and hence it should replace the element in the first position of the array by 1 is it?because we are altering the same i(same memory location) on the both sides is it?

can u assist me please?


regards
30081986
Nov 3 '07 #4
JosAH
11,448 Expert 8TB
Thanks for the reply..
I have a small doubt..Since we are using the same 'i' as the index to access the array elements,shouldn't reflect back on the left hand side when the decrement operation is done on the right hand side..i.e we are using 'i' as the index to access the array elemts and 'i' becomes 2 first on the right side and the same 'i' becomes 1 on the left hand side ,so effectively i now has 1 and arr[i] is now arr[1] and hence it should replace the element in the first position of the array by 1 is it?because we are altering the same i(same memory location) on the both sides is it?

can u assist me please?


regards
30081986
Let's go through this code fragment step by step:

Expand|Select|Wrap|Line Numbers
  1. int i= 1;
  2. a[++i]= --i;
  3.  
- first i is initialized to the value 1, the easy part.

- Java evaluates everything from left to rightl no exception to the rule so first
++i is evaluated; the result is 2 and that's the value of i as well.

- a[2] is the target of the assignment.

- next --i is evaluated and its value is 1 which is the value of i as well now.

- a[2]= 1 is evaluated; it is an assignent so a[2] is set to the value 1.

Does that clarify things a bit more?

kind regards,

Jos
Nov 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: johnny | last post by:
hello. in C++, you can fake a sub-array by passing something like (array + int). If this was passed to something that takes an array as a parameter, the function will deal with the array using...
3
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
4
by: Tydr Schnubbis | last post by:
I've read an interview with Brian Kernighan here: http://www-2.cs.cmu.edu/~mihaib/kernighan-interview/index.html. The interviewer asks him, among other things, about the weaknesses of C. This is...
15
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with...
3
by: John Bend | last post by:
Hello. Can anyone please suggest some good tutorial links where Java Vectors and Arrays are explained and compared? I'm a proficient programmer but fairly new to Java. Whilst I understand...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
9
by: jyohere | last post by:
Can anyone explain in detail why arrays are implemented as objects in java.And wat do we mean by arrays of arrays in java.....am not getting it completely....how does arrays differ from other...
1
by: Newbie19 | last post by:
I'm just learning java arrays/arraylists and was wondering what are the best books for learning java arrays/arraylists? I know practice is the best way to learn, but I have a hard time...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.