How String Objects Are Different From Other Objects

Jan 23, 2015

how String objects are different from other objects

part 1:

// creating two objects
Dog mydog1 = new Dog();
Dog mydog2 = new Dog();
// comparing the reference variables
if( mydog1 == mydog2){
System.out.println(" The reference variables refer the same object ");
}
else {
System.out.println(" They refer to different objects ");
}

The above code works as I understand objects , it prints "They refer to different objects " to the screen.

Part - 2

// creating two objects ( I beleive, pls correct me if i am wrong )
String a = "haai";
String b = "haai";
 
if( a == b){
System.out.println(" Reference variables refer to same object");

When i run the above code it prints that a and b refer same object , I don't understand how they refer to same object when i didn't assign " String b = a; ". My question is did java just create one object and stored the same reference values to a and b .

View Replies


ADVERTISEMENT

Copying Data Objects To Serializable Objects

Jul 27, 2014

I am currently working on a project where I need to return data from a database over RMI to a client who requests it. Some of the fields in the Data Object can not be seen by the client so I need to create another object to send over the network instead. The method I use is this...

public static SerializableObject createSerializableObjectFromDataObject(DataObject dataObject){
SerializableObject serializableObject = new SerializableObject();
serializableObject.setField(dataObject.getField());
serializableObject.setAnotherField(dataObject.getAnotherField());
return serializableObject;
}

Is there a better way of doing this? I am creating many subclasses DataObject which all require this static method to be implemented and I can't push it into the superclass because each one needs custom behaviour.

View Replies View Related

How Many String Objects Are Created In Each Line

Aug 15, 2014

how many objects got created at each line (in String pool or in Objects memory):

public class Test {
public static void main(String[] args) {
String s1 = "test";
String s2 = new("test");
String s3 = s1 + s2;
StringBuider sb = new StringBuilder().append(s2);
}
}

View Replies View Related

How Many Objects Created With New String Method

May 22, 2014

I am little confused about String creation in java.

Doubt 1: How String objects assigned to Pool area:

1. String s="in pool";
2. String s1= new String("not in pool");

How many objects created in statement 1 and 2. According to recent discussion with my colleague, one object created in String pool in case 1. And in case 2, two objects are created, one as literal goes to String pool and other with new() opr goes to Heap.

If above is correct, Ain't we wasting double memory for same object ? Really need clear understanding on this

Doubt 2: How does intern() work: Please see if my below explanation is correct

1. If String literal is already present in String pool , and i create a same string with new operator, reference to object is changed to pool area.

2. If String object is created using new operator and intern is called on it. If same string object is not present in the String Pool, Its moved to String pool and reference to this in Pool is returned.

View Replies View Related

Web Services :: Large Number Of String Objects

Apr 20, 2015

I have application written in rest with Jersey-Jackson for JSON processing. All the resources produce and consume JSON. Now, the problem is, it is a server intensive application and large number of request will be hitting the server with large JSON request object. Now, because of this reason, when the JSON object gets converted in to Java object with String fields in it mapping to JSON request values large number of string objects are getting created which is resulting in frequent GC.

View Replies View Related

Create Separate String Objects As Strings Are Immutable

Jan 28, 2015

I want to clarify it whether this below code, when running this loop, will it create separate string objects as strings are immutable or else, will it keep the same reference(as each time inside loop, we use the same name 'rslt') and assign new string value for this?

while(inc < numberOfColumns){
String rslt=rs.getString(inc+1);
rowArr[inc] = rslt;
inc++;
}

View Replies View Related

Sorting Array Of Objects Based On One Of Class String Variables

Apr 8, 2014

I have a school assignment that involves me sorting an array of objects based on one of the class String variables. I am using as the title says a simple selection sort method. The problem I'm having is that when I run the program in debug mode, it never seems to enter the if statement in the inner loop. I would like to say I've tried a number of things to figure it out, but honestly I'm just stumped as to why it's not working.

Here is the code:

public static void sortTransactions(Transaction[] oTransaction){// This is the sorting method, obviously it's not done so it currently just prints to screen.
System.out.println("Successful call to sortTransaction()");
String min = "";
int curInd = 0;
Transaction[] temp = new Transaction[1];

[Code] ....

The output when I check to see if the array is sorted verifies that the array never does get sorted.

View Replies View Related

Real Reference Values (memory Locations) Of 2 String Builder Objects

Mar 7, 2014

My intention is to see the real reference values (memory locations) of 2 StringBuilder objects as below:

StringBuilder sb2 = new StringBuilder("123");
StringBuilder sb3 = sb2;
System.out.println(sb2);
System.out.println(sb3);

I was expecting a printout like

@S234853
@S984724

But all I got was 123 printed twice. How can i print out the reference values instead of what is inside the StringBuilder object ?

View Replies View Related

Update XML Using DOM Objects

Sep 29, 2014

I have a requirement to insert the data into xml file . I have used DOM class for doing this. Below is my xml file

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<deployments>
<applications>
<application>
<name> PCMH</name>

[Code] ....

The thing is if SITA is the name then I have to insert component to the particular envi tabs , if DEVB is the component I have to insert the component there. How can I do this I have done some code its inserting the data at the bottom og the XML .

View Replies View Related

How To Get Two Classes To Get Objects From Each Other

Oct 18, 2014

I'm quite new to Java. I have some trouble with understanding how to get two classes to get objects from each other (if that is the correct term).

Lets say I have a login class, in which I have a method checking what the user has entered into the console (I have not displayed the whole code, but this class works as it should and give the user an option to enter username and password and return it true if entered correct).

public static boolean validateUserPass(String userName, String password) {
String[] user = {"admin"};
String[] passwords = {"firkanten"};
boolean check = false;
for (int i = 0; i < user.length; i++) {
if (userName.equals(user[i])) {
if (password.equals(passwords[i])) {
check = true;

Now, in another class I want a display box to appear on the screen and give the user three options: Click yes, no or cancel. I get this to run perfectly alone, this is not the hard part for me. I want the display box only to appear when the correct username and password is given, but I can't seem to figure out how to do this probably.

View Replies View Related

Getting Names Of Objects?

Jan 22, 2015

In my project I had to create 2 classes, Room and Animal.

Room had to have an array (NOT arrayList-I know theyre better and easier but I need to use an array, for now) This array must be able to be populated by 10 "Animals", and the Room class needs two methods, a method to add animal a to the room (rooms array) and a toString() that returns the name of the room and the names of the animals in the room. The teacher added a hint saying that this toString() should reference the animal classes toString()

Here is my code thus far:

public class Room {
private String name;
Animal[] inRoom = new Animal[10];
public Room(String roomName){
name = roomName;
} public void addAnimal(Animal a){
for(int i = 0; i < 10; i++){

[code].....

View Replies View Related

XML Editing Using DOM Objects?

Sep 29, 2014

I have a requirement to insert the data into xml file . I have used DOM class for doing this.

Below is my xml file

<code>
-<deployments>
-<applications>
-<application>
<name> PCMH</name>
-<envs>
-<env>
<name> DEVA</name>
<version>1.0.0 </version>

[code]....

The thing is if SITA is the name then I have to insert component to the particular envi tabs , if DEVB is the component I have to insert the component there. How can I do this I have done some code its inserting the data at the bottom og the XML.

View Replies View Related

Objects Being Displayed On Top Of One Another?

Dec 15, 2014

I am making a game in java and for the game board i want to fill the screen with blocks. to do this i stored objects of a class that displays squares into an array list and displayed the array list. however, when i do this all of the squares are drawn on top of anther at the final squares coordinates and i dont know why.

here is the code

// code for adding the squares into the array
nt x = 0;
int y = 0;
int size = 10;
static ArrayList<Map> map = new ArrayList<Map>();
//static ArrayList<Items> items = new ArrayList<Items>();

[code]....

View Replies View Related

Can't Create New Die Objects?

Apr 2, 2015

I'm having trouble creating two new die objects for the PairofDie class. I'm trying to run two separate die and print the face value and then add both numbers up and print those values as well.
 
public class Die {
private final int MAX = 6;
private int faceValue;
//private int faceValue2;
public Die(){
faceValue = 1;
 
[Code] .....

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

The method Die() is undefined for the type PairofDice
The method Die() is undefined for the type PairofDice
at PairofDice.main(PairofDice.java:6)

View Replies View Related

Iterate Through Objects

Feb 27, 2015

I would like to know how I can iterate through objects . I have a manually created linked list (without using the built-in method one). So in the memory my object looks like this(attachment).I would like to do a for loop or while loop to get each element under the test3.head.

View Replies View Related

Array Of Objects

Mar 28, 2014

Exercise the Coin class using a driver class in which 5 coins are flipped, as a group, 1024 times. Display the number of times exactly 4 tails are obtained. Display the number of times exactly 5 tails are obtained. Use an array of Coin.

how to put an object into an array?I know how to make a coin class with a Heads and Tails, but I dont know how to make 5 coins all at once.

View Replies View Related

How To Compare Objects With Another

Feb 3, 2014

I have the following method stubs to complete:

public class Item{
private int myId;
private int myInv;
public Item(int id, int inv){
myId = id;
myInv = inv;

[Code] .....

I do not understand how to complete the compareTo() method and the equals() method. I understand that the compareTo() to should return 1 if the argument is smaller -1 if its bigger and 0 if they are the same, but I do not understand how it's possible to compare the current object with the object passed in as the parameter.

This is what I tried for the compareTo():

public int compareTo(Item other){
return this.compareTo(other);
}

But my compiler tells me its a recursive call so I am not sure if I am doing this correctly.

Basically I am supposed to compare an object to another but I am not sure how to access the object that is not passed in as a parameter and compare it. Same goes for the equals(). This is what I tried for that (compiler also says its a recursive call):

public boolean equals(Item other){
return this.equals(other);
}

View Replies View Related

How Many Objects Are Created

Jan 19, 2014

String s= new String ("Hello");
String a = new String ("Hello");

As per my understanding the first line creates 2 objects : 1 for the heap memory and 1 for String pool

2nd line : new object created and no new object created in the String pool as the keyword already exists in the pool.

So the total number of objects created after the execution of 2 lines are : 3

View Replies View Related

Unwanted Changes In Objects

Nov 22, 2014

I can't come up with a simple scenario so I'll use my actual code to explain the problem, and it's going to be related to java.awt.Color, but my problem is general, not awt dependent. Although I will shorten my code to show only what I think is relevant to the problem.

Java Code:

public class GameColor {
public abstract class Effect {
public int strength;
public Effect(int strength) {
this.strength = strength;
}
public abstract Color get(Color color);

[Code] ....

I have my reasons for having a SetHue inner class and a setHue method that practically do the same thing. The inner abstract class Effect and inner class SetHue was static but I changed it when I testing what could cause this problem, later I will need them to be static, but since they don't affect the problem I don't think it matters if they're static or not.

So down to the problem, I create three new GameColor instances which all are 255, 0, 0, basically red. Then I do setHue(0) method on the first one, setHue(20) on the second one, and setHue(40) on the last one. And for some reason all of them have a hue of 40, whereas the last one should've had this, so basically the last time I call setHue on the instances of GameColor, changes the hue of all of these colours to that hue. I want it to be individually affected, but all of them are affected. This is where I apply these changes:

Java Code:

colo1.setHue(0);
colo2.setHue(20);
colo3.setHue(40); mh_sh_highlight_all('java');

And how I initialise the colors:

Java Code:

public GameColor colo1 = GameColor.RED;
public GameColor colo2 = GameColor.RED;
public GameColor colo3 = GameColor.RED; mh_sh_highlight_all('java');

View Replies View Related

Using Objects In HashMaps

Mar 21, 2014

I am currently trying to implement a hashmap from a class i currently have that is called 'Paper'

My paper class consists of:

String Title;
String[] author;

What I am wondering, is if there is anyway I can call these seperate attributes to be both the key and the value of the hashmap. Currently I cannot see a way to call individual values, only the class itself.

View Replies View Related

Compare All Objects In ArrayList To All Others But Not Self

Mar 15, 2015

I have a ArrayList of objects of class called HockeyPlayer (ArrayList<HockeyPlayer>). A HockeyPlayer has a String name and int number of goals.

This is my current work for comparing each object in the list to every other and printing them to screen:

Iterator<HockeyPlayer> it = hockeyPlayersList.iterator();
while (it.hasNext()) {
HockeyPlayer singleHockeyPlayer = it.next(); //the first one encountered
// HockeyPlayer nextHockeyPlayer = it.next(); //the next one encountered

[Code]...

This of course produces some duplication in the print-out: These are the equal hockey players: Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.

Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.

These are the equal hockey players: Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.

Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.

How do I remove the duplication in the print-out?

View Replies View Related

How To Find Particular Objects In HashMap

Dec 25, 2014

Suppose i have a hashMap which includes instances of class Employees and instances of class Customers.

How can i get the employees objects only?

And would it be possible to find the oldest staff by comparing the ages stored in the age fields of the staff objects.

View Replies View Related

Accessing Objects From Other Packages

Dec 7, 2014

I have GameConsole class with gamePlay(). There are two objects in this class that I want to access from a class in another package.

package blackjackControls;
public class GameConsole {
Player dealer; //both of these objects I am trying to bring over into the blackjackGUI package
Deck playingDeck;
public void gamePlay(){

[code].....

The dealer and playingDeck objects are giving me an error of unresolved. the way it is written I also get a static error on line 37. I know that I have not yet written in the actionEvent statement in the button constructor.

View Replies View Related

JSP :: ForEach ArrayList Of Objects

Feb 11, 2014

I am trying to access an array of "Movie" objects in my jsp. The array is loaded via

org.springframework.web.servlet.ModelAndView.addObject().

Here is my jsp code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

[Code] .....

The System.out.println("jsp page: .... &> results in the output: "jsp page: movielist - [Title: Die Hard; Budget: 20000000, Title: two days in paris; Budget: 1000000]" so I am confident the objects are being loaded into the ModelAndView correctly. However the output of the block is "${movie.name}" instead of the list of movies. My movie object has a getName() method to return a string (and a setName() method). I am not sure why the System.out.println statement can find the movielist attribute, but ${movie.name} is being treated a plain text. There are no execptions thrown or other indications of errors.

View Replies View Related

How To Clone ArrayList Objects

Jun 20, 2014

I want to clone some Arraylist, but the compiler apparently are just referencing the values to it's original ArrayList. I don't know what should it be:

package projetoteste;
import java.util.ArrayList;
import java.util.List;
public class TestFood {
public static void main(String[] args) {
List food=new ArrayList();

[code]....

Notice that ArrayList dailyMeal should be untouchable, but it return the changes that I made in local for-loop iteration although I didn't added nothing to it, just to it's clone.

Output:

[[[Suco, 1 , 2], [Suco, 1 , 2]], [[Suco, 1 , 2], [Suco, 1 , 2]]]
[[[►, ►, Suco, 1 , 2], [►, ►, Suco, 1 , 2]], [[►, ►, Suco, 1 , 2], [►, ►, Suco, 1 , 2]]]

View Replies View Related

Accessing Objects Within GCompound?

Sep 19, 2014

Given the class MyExample below:

public class MyExample extends GCompound {
//instance variables
public GRect R1 = new GRect(0, 0, 20, 20);
public GRect R2 = new GRect(0, 0, 5, 5); //R2 is in front of R1, but its coordinates are all "inside" of R1's coordinates
//constructor
public MyExample() {
add(R1);
add(R2);
}
}

1) Suppose I'm in a GraphicsProgram and declare:

MyExample myex = new MyExample();

Suppose I have coordinates (x1, y1), and want to find out whether this is the myex object defined in the previous line. I would use:

obj = getElementAt(x, y);
if (obj == myex) (...and so on)

Now suppose I want to test whether the object is the GRect object R1 within myex. Why doesn't the following work?

if(obj ==myex.R1) (...and so on);

Here is the full code that shows my question; it outputs "myex", none of the other outputs come out...

public void run() {
GObject obj1, obj2;
MyExample myex = new MyExample();
add(myex);
obj1 = getElementAt(1, 1);
obj2 = getElementAt(19, 19);
if (obj1 == myex) System.out.println("myex");
if(obj1 == myex.R1) System.out.println("R1");
if(obj1 == myex.R2) System.out.println("R2");
if(obj2 == myex.R1) System.out.println("R11");
if(obj2 == myex.R2) System.out.println("R22");
}

View Replies View Related







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