Passing Reference Data Type Arguments

Oct 21, 2012

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.For example, consider a method in an arbitrary class that moves Circle objects:

public void moveCircle(Circle circle, int deltaX, int deltaY) {
// code to move origin of
// circle to x+deltaX, y+deltaY
circle.setX(circle.getX() + deltaX);
circle.setY(circle.getY() + deltaY);

// code to assign a new
// reference to circle
circle = new Circle(0, 0);
}

Let the method be invoked with these arguments: moveCircle(myCircle, 23, 56)

Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

View Replies


ADVERTISEMENT

Passing Primitive Data Type Arguments

Jun 5, 2014

I am totally new to Java. What is the purpose of this method?

Flow of the int x=3; like where does the 3 go step by step?

Passing Primitive Data Type Arguments (from oracle java tutorials)

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost. Here is an example:

public class PassPrimitiveByValue {
public static void main(String[] args) {
int x = 3;
// invoke passMethod() with
// x as argument
passMethod(x);

[Code] ....

View Replies View Related

Java And Passing By Reference

Nov 12, 2014

I know in C++ It's possible to pass by reference but what about java?

For example, can i pass the address of a health variable into a Ninja class and then

Since Ninja inherits from an enemy class pass the health into the enemy class and within there have a function that returns that same health address and takes 5 from the health returning 95.

View Replies View Related

Cannot Infer Type Arguments For ArrayList

May 1, 2014

At the beginning of one of my classes, I instantiate some List objects. For example ...

Java Code: private List<> byName = null; mh_sh_highlight_all('java');
... then later, I reference this again in a method like this:

Java Code: byName = new ArrayList<>(rs.getString("firstName"), rs.getString("Last Name")); mh_sh_highlight_all('java');

This is where I get the error. I've tried making the list of <String>, but that didn't make a difference.

View Replies View Related

Type Class Is Not Generic - It Cannot Be Parameterized With Arguments

May 16, 2008

I have set up a project in Eclipse 3.1 and am using java 5.0 compiler.

Here's my folder structure in Eclipse

Java Code:

DFSRemoteClientTestClient.java mh_sh_highlight_all('java');
DFS is the project in Eclipse

And this is how it looks my java class

Java Code:

package RemoteClient;
import java.util.*;
// other imports
public class TestClient {
public static void main(String [] args) throws ServiceInvocationException {
// business logic here ....
}
} mh_sh_highlight_all('java');

So, basically, my java class is just a simple class with a main function.

Now when I build my project, using Project->Clean...

Then I get this as an error at the very first line where i specify the package

This is the error:

Java Code: The type Class is not generic; it cannot be parameterized with arguments <T> mh_sh_highlight_all('java');

What's this error and why am I getting this.

View Replies View Related

Int Is Not A Reference Type

Jan 24, 2015

I don't really know what this means and it is sending an error when I try to run my program I am trying to set the x and y value (Int x and int y) to setVisible false at a specific time in my game but it keeps sending the error that int is not a reference type.

View Replies View Related

Purpose Of Type Associated With A Reference?

Mar 29, 2014

When I write a statement such as:

myType x;

This means that x is a reference to objects of type, "myType" and no memory has been set aside for an instance of this object. I think it's true that x also has the starting memory address of this object.

What I feel uncertain about is if x is telling the JVM(?) where it can find the starting address of this object, why do we care about the type that x is?

i.e., if we assign x to an object totally unrelated to this type then the compiler would complain - correct? If so, how come, since all x is doing is telling the JVM(?) where to find your object (via the address it contains).

I suppose all, hy a reference has a type associated with it if all the reference does is point you to the object?

View Replies View Related

Accepting User Input Of Reference Type

Nov 16, 2014

I have a Homework that's on Polymorphism and inheritance. The problem is of a triangle class. The super class is GeometricObject and the subclass Triangle. I have the parent and child classes compiling, now it's the test program.

The problem is the examples in the book don't show anything about prompting input for objects. The objects in the Triangle class are: side1, side2, and side3 and so I created objects in the test: Triangle sideOne = new Triangle(); . I've tried to compile but jGrasp doesn't like anything I do and the instructor in my class hasn't shown us examples yet. Can I prompt input for reference type and not just primitive type?

View Replies View Related

Cannot Assign Generic Object Reference To Similar Type

Jul 26, 2014

I have the following code:

public class CollisionManager<T> {
private boolean collision = false;
private T mainEntity;
public <T extends Entities> void handleCollision(T mainEntity, T secondEntity){
this.mainEntity = mainEntity; // This is illegal.
}
}

Why "this.mainEntity = mainEntity" is incorrect and also show me the correct way to achieve this?

The error I am getting is "Type mismatch: cannot convert T to T"

View Replies View Related

Implement Equality And HashCode Method If Class Has Reference Type Members?

Jan 16, 2015

I am trying to implement the following example to override the equality and hashCode method if the class has reference type member. I do get the expected result "true" for equal and "false" for non-equal objects. But the print statement in the Circle's equal method is not executed when the objects values are not equal. I don't know what i am missing, though i get the equality result "false" as expected for non equal objects.

class Point{
private int x, y;
Point (int x, int y) {
this.x =x;
this.y = y;

[code]....

View Replies View Related

Passing Unknown Type To A Method And Returning It

Feb 20, 2014

So I want to know how in Java you can pass a unkown type into a method (type can be an int, double, or a user defined object) and return that unkown type.

example of what I want:
Java Code:
public (unknowntype)[] method2 ((unknowntype)[])
//Process Data
//unknowntype.process();
return (unknowntype);
} mh_sh_highlight_all('java');

I know in C you can use void pointers and in c++ we have templates but I do not know how java handles this. Also I want to know if it is possible to call a method in the unknowntype.

View Replies View Related

Method Is Declared To Take Three Arguments - Only Two Arguments Pass On Program Call

Jun 9, 2014

A method is declared to take three arguments. A program calls this method and passes only two arguments.Will it take the third argument as

1 null
2 void
3 zero
4 Compilation error

View Replies View Related

Null Is What Type Of Data Type

Sep 3, 2007

method passing null to the called function, compiler would take that parmer as what type of paramer???

View Replies View Related

Servlets :: Cannot Make Static Reference To Non-static Method GetQuery From Type Resource

Mar 26, 2015

This is my code inside the method:

@Post
public static String getDetails(Representation entity) throws Exception {
String customerId = getQuery().getValues("cus_id");
}

I use this code in Restlet Representation. I try to get the value from the Request API. But I am facing the problem as "Cannot make a static reference to the non-static method getQuery() from the type Resource".

View Replies View Related

Inheritance Relationship Between Type Of Actual Object And Object Reference?

Apr 15, 2014

For example I create an object like this:

BankAccount b = new SavingsAccount();

Now lets say that I want to access a method 'addInterest()' that is in the 'SavingsAccount' class I would have to do: '((SavingsAccount)s).addInterest();'

The question I have is why do I have to cast 'b' to SavingsAccount? Isn't the actual object reference of 'b' already an instance of 'SavingsAccount' class? How does the 'BankAccount' affect the object itself? I'm really confused as to what class is truly getting instantiated and how BankAccount and SavingsAccount are both functioning to make the object 'b'.

View Replies View Related

Object Reference Variable Unwilling To Be Reset To A Different Object Type

Nov 6, 2014

I don't understand why the object reference variable 'a' cannot be recast from a thisA object reference to a thisB object reference.Is it the case that once a reference variable is linked to a particular object type then it cannot switch object types later on.I am facing the Java Associate Developer exam soon and I am just clearing up some issues in my head around object reference variable assignment,

class thisA {}
class thisB extends thisA { String testString = "test";}
public class CastQuestion2 {
public static void main(String[] args) {
thisA a = new thisA();
thisB b = new thisB();

[code]....

View Replies View Related

Cannot Make Static Reference To Non-static Type String

Apr 27, 2014

I am writing the following program in Java SE 7. It throwing "Cannot make a static reference to the non-static type String" . However if I write parameterised String inside main method as  java.lang.String[] args, it compiles fine.
 
class MainClass<String> {
  <T> MainClass(T t) {
   System.out.println(t.getClass().getName());
  } 
  public static void main(String[] args) {
  System.out.println("asdasd");
   new MainClass<>("");
  }
}
 
I mean following programs compile fine in Java SE 7 :
 
class MainClass<String> {
  <T> MainClass(T t) {
   System.out.println(t.getClass().getName());
  }
  public static void main(java.lang.String[] args) {
  System.out.println("asdasd");
   new MainClass<>("");
  }
}

View Replies View Related

Passing Data From One Action Event To Another

Oct 5, 2014

this is the first code

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ChickenListener implements ActionListener {
int loopctrl;
static Connection cn;
static Statement st;
static ResultSet rs;
static PreparedStatement ps;
 
[Code]...

the second code

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class QuantityCounterListener implements ActionListener {
String tempStrng;
int tempInt;
int x = 0;

[Code]...

what my problem is i need to transfer what does "tempString" from code 1 holds to code 2 in the //x part in the prepared statement.

View Replies View Related

Swing/AWT/SWT :: Passing Data Through Globally Created Table Variable?

Jun 14, 2014

I have created a DefaultTableModel tablel as a Global variable. The table is then created and attached to a Grid bag layout. Then I want to call the table again in another method to add rows of data into it. Hopefully that makes sense.

So the addrow for the table is located in the final method private class CalcButtonListener implements ActionListener

When I debug the code, deftablemodel variable is carrying NULL data.

Also to make things even more complicated, The actual headers for the table aren't showing up,... not entirely sure why though.

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JMenuBar;

[code]....

View Replies View Related

Data Type That Can Store Methods

Mar 21, 2015

I was wondering is there any data type that can store method?

View Replies View Related

Using Boolean Data Type In Program

Sep 25, 2014

While practicing java i have came across boolean data type. i have executed below program but i am not sure how i got output of the program like below:

10 > 9 is true

Here is my program:

class BoolTest3
{
public static void main(String args[])
{
System.out.println("10 > 9 is "+(10>9));
}
}

My question is: how this program will come to know that 10>9 true or false.. how bolean data type used in this program.

View Replies View Related

Creating Array With Multiple Data Type?

Oct 10, 2014

i need to create an array with attributes name, gender, phone, age.and then sort acording to age in ascending order.

i created like this,

public class Array{
private String[] name={"ram", "katy", "priti", "john"};
private String[] gender={"male","female","female","male"};
private int[] phone={989898089,89898989,8982989089,898908989};
private int[] age={45,24,30,28};
  public void printarray(){

[code]....

This code sorts the age attribute alone and when printing ouput it swaps one person's age to other person, how to make it correct

View Replies View Related

Double Data Type Outputting To Exponent?

Nov 9, 2014

I was struggling to use BufferedReader to extract some data and then perform some calculations and then have the results as outputs.

I haven't quite solved that issue but in order to progress, I hard coded some values into my application and proceeded with the actual calculation loops etc.

Currently, the value out put from one of my calculations is given as:

1.1704926E7

How can I make the console show it in a natural way. I've performed the calculation manually and it should be 11704926.5 I don't want to lose that .5!

View Replies View Related

Unable To Use Double Data Type In For Loop

Aug 12, 2014

I am trying to use double data type in a for loop for precise operations and just to see if there could be any problem doing that I tested a small code :

public class doubleLimit {
public static void main(String[] args){
for(double i=-0.1;i<=0;i+=0.01)
System.out.println(i); }}

The output I was expecting is :
-0.1
-0.09
-0.08
-0.07
-0.06
-0.05
-0.04
-0.03
-0.02
-0.01
0.00

But the output of the code is :
-0.1
-0.09000000000000001
-0.08000000000000002
-0.07000000000000002
-0.06000000000000002
-0.05000000000000002
-0.040000000000000015
-0.030000000000000013
-0.02000000000000001
-0.01000000000000001
-1.0408340855860843E-17

Why is the code not working the way I expected, I think it has something to do with any property of double but I am not sure.

View Replies View Related

Adding Any Type Of Data To A Generic List?

Aug 28, 2014

Is this the proper way to add to a generic list? My code works just fine, but I got this feeling that there might be some kind of flaw in it or something. Is this pretty much the basic way to add any type of data to a generic list?

import java.util.LinkedList;
public class ListOfGeneric<E> {
private LinkedList<E> myList;

ListOfGeneric(){
myList = new LinkedList<E>();

[Code] ....

View Replies View Related

Store Data From A File Into Array Of Type Product?

Mar 17, 2015

public class InputFileData {
/**
* @param inputFile a file giving the data for an electronic
* equipment supplier’s product range
* @return an array of product details
* @throws IOException
*/
public static Product [] readProductDataFile(File inputFile)
throws IOException{
// YOUR CODE HERE
}

This code is meant to be used to read a text file and store the data in an array of type Product[]. I know how to read in a text file and have it sort it into an array, but I've never seen code laid out in this fashion before (specifically "public static Product[]", and I'm unsure how to work with "(File inputfile)". I've looked all over the place but can't find any examples of anything like this.

Also, the code given cannot be changed, as it's that code I have to work with.

I still don't understand how to use it as a whole. For example, do I read the file in the main and have this method read that in and output to the Product class? Do I read the file in this method? I can't work out how to make this work when I have to use this method.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved