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


ADVERTISEMENT

How Do Memory Allocate Space For Objects Or Variables

Sep 1, 2014

I'm new to programming and I would just like to know what happens inside the memory once you run a java application. How do the memory allocate space for your objects or variables? Where does it start?does it start in memory location 1 first when I create my first variable or object? What is the use of variables and how is it used by the memory?Also if I create a variable named String s1 then I created another variable again named s1= "myString" Did I create a new object or I just edited the value of the s1 variable??

And if I create a variable int num1 = 10 then I created another variable int num1 = 12 did I create another one or I just edited the num1 variable?? Also how much memory would an object have? Does it depend on how many primitives objects like arrays or strings it have?Also I want to develop 3D games also, in the future is Java going to be great as I know machines in the future will have massive amounts of RAM and the JVM will be better...

View Replies View Related

JavaFX 2.0 :: Objects Don't Free Heap Memory

Apr 9, 2014

I have a simple JavaFX Application that open a Browser and shows google page. After exit the Application and free all objects, I can see that the JavaFX objects like Scene, Stage, WebView and WebEngine are alive in the heap memory after call GC. I can see this objects with JProfiler and other Profiler tools.

This is my Test code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.application.Application;
import javafx.application.Platform;

[Code] .....

And my JavaFX Application:

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.layout.Region;import javafx.scene.paint.Color;

[Code] .....

To test the application click on Start Button to show google web page, click on Stop Button to stop the application, run a Profiler tool, and call gc, the JavaFX classes are alive. I am using java version "1.7.0_51" and windows 8.1 Is there something wrong in my code? Or this is the normal behavior?

View Replies View Related

Using This Reference Variable On Constructors - Involve Objects?

Apr 2, 2014

I am trying to simplify my constructor by using "this" reference variable. I am aware how to use this reference when making constructors that involve fields of primitive data types; however, not so sure fields involving objects. I have made the following constructors and haver placed a question mark in the last parameter. How I can use this reference that involves objects.

public class RetailItem
{
//Create the fiels for item.
//You need item name and item number. then you would need
//cost
private String description;
private int Item_Number;
private CostData cost;
 
[Code] ....

View Replies View Related

Computing New String - Memory Out Of Heap Error

Sep 28, 2014

Given a string, compute a new string where identical chars that are adjacent in the original string are separated from each other by a "*". My implementation :

package com.tcs.dash;
public class StringBuild {
public String edit(String userIp){
StringBuilder builder = new StringBuilder(userIp);
String replaceText = "";
for(int i = 0; i < builder.length() - 1; i++){
if(builder.charAt(i) == builder.charAt(i+1)){
replaceText = builder.charAt(i) + "*" + builder.charAt(i+1);
builder = builder.replace(i, i+1, replaceText);
}
}
return builder.toString();
}
}

I am getting error at line 13. An exception actually.

I/P given = aaaa

Console:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.replace(Unknown Source)
at java.lang.StringBuilder.replace(Unknown Source)
at com.tcs.dash.StringBuild.edit(StringBuild.java:13)
at com.tcs.dash.StringBuildExample.main(StringBuildExample.java:14)

View Replies View Related

String Reference Equality

Jan 23, 2014

I don't understand why following code when executed returns false
 
String s1= " string".trim();
  if(s1 == "string"){
  System.out.println("true");
  }
  else{
  System.out.println("false");
  }

View Replies View Related

Setting Buttons / Labels / Text Field To Exact Locations

May 1, 2015

I need to format the items(buttons, labels and textfields) to make them look like the picture in the attachment.Here is my code:

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

[code]....

View Replies View Related

Passing Values And Objects

Oct 28, 2014

Whether I pass primitives or objects the original value does not change. Is this expected result?

Java Code:

public class Class {
public static void main(String args[]) {
int x=3;
doItPrim(x);
System.out.println(x);//3
Integer i=new Integer(3);

[Code] ......

View Replies View Related

Objects Keep Printing Null And Zero Values?

Nov 3, 2014

Everything is written up and looks good and i get no compile errors but every student object other than the default constructor has null and zero values when printed.
 
import java.text.DecimalFormat;
import java.lang.Math;
public class Student
{
//instantiate variables
 
[Code] .....

View Replies View Related

How To Give Values To Array Objects Without Using Loop

Apr 3, 2014

The main method should creates a Student object with name as "Bill" and marks as {88,92,76,81,83} and print it.

Java Code:

class TestStudent {
public static void main(String args[]) {
Student1 st = new Student1();
st.name = "bill";
Student st1[] = new Student[6];
//This gives error
st1[].marks = {1, 5, 8, 9, 7, 6}; mh_sh_highlight_all('java');

View Replies View Related

Creates Two Die Objects And Roll Them Display The Values

May 4, 2014

I've written a die program, and i want the user to type any key in order to continue, how do i achieve this ?

I have already created a Die class, i just want to implement the main method now.

PHP Code:

package test;

/* This program demonstrates the use of a user-defined class

public class RollingDice {
// Creates two Die objects and rolls them display the values.
public static void main (String[] args) {
Die die1 = new Die();
System.out.println("Roll the die ")
}
} mh_sh_highlight_all('php');

What should i add after System.out.println, So that the user types (any key, or types "Enter" for example, for the die to roll). i dont want the die to roll by itself i Want the user to interact with the program in order for it to roll.

View Replies View Related

Allow User To Enter Values For Array Of Seven Salesperson Objects

May 6, 2014

Create an application that allows a user to enter values for an array of seven Salesperson objects. Offer the user the choice of displaying the objects in order by either ID number or sales value.

import java.util.*;
public class SalesPersonSort
{
public static void main(String args[]) {
int[] id = new int[7];
int[] salesValue = new int[7];
final int END = 999;

[Code] ....

View Replies View Related

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 View Related

Modify Values In Fields In Serializable Object Using Objects Set Methods Java

Dec 10, 2014

This program is basically complete. It compiles and runs. It is a college course assignment that I pretty much completed but for the last part in which I'm suppose to change the values of all fields and display the modified values using a toString method. Modifying the values of the fields is where I am stuck. I don't think I need to create a new text data file to do this. The instructor only asked that all the values of fields be changed and this was the last part of the assignment so I don't think it involves creating additional ObjectOutputStream and ObjectInputStream objects. I'm getting a NullPointerException error on line 161.Here is the code. I'm also including the input data file.

//create program so that objects of class can be serialized, implements interface Serialiable
//create constructor with 4 parameters with accompanying get and set methods, Override toString method
//create text file with 5 records, create Scanner object,ObjectOutputStream, and ObjectInputStream
//create new ItemRecord object, change values of all fields in ItemRecord object using object's set methods
//modify ItemRecord object using toString method

[hightlight =Java]import java.io.Serializable;
public class ItemRecord implements Serializable

[Code] .....

This is the error message:

----jGRASP exec: java ItemRecordReport

Item Cost Quantity Description
Number
A100 $ 99.99 10 Canon PowerShot-135
A200 $149.99 50 Panasonic-Lumix T55
A300 $349.99 20 Nikon- D3200 DSRL
A400 $280.99 30 Sony- DSC-W800
A500 $ 97.99 20 Samsung- WB35F
Exception in thread "main" java.lang.NullPointerException
at ItemRecordReport.main(ItemRecordReport.java:161)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Here is the data file:
A100 99.99 10 Canon PowerShot-135
A200 149.99 50 Panasonic-Lumix T55
A300 349.99 20 Nikon- D3200 DSRL
A400 280.99 30 Sony- DSC-W800
A500 97.99 20 Samsung- WB35F

Here is the data file for the modified field values.
B100 98.00 10 ABC1010
B200 97.00 15 DEF1020
B300 96.00 10 GHI1030
B400 95.00 05 JKL1040
B500 94.00 01 MNO1050

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

Raising A Real Number To Negative Value

Jan 21, 2014

I am trying to raise a real number to a negative value e.g x-y(x raised to power -y) which can also be written like 1/xk . I am trying to run th program each time bt it doesnt give me the desired result.

Here is the code

public class RaiseRealPower {
public void run(){
double value =readInt("Enter value ");
double power =readInt("Enter power ");
System.out.println("Answer is " +raiseIntToPower(value,power));

[Code] ....

View Replies View Related

Real Purpose Of Using Protected Access Specifier

Jan 26, 2014

What is the real purpose of using Protected access specifier in Java?

View Replies View Related

Effect Of Calling Real Service Call In Test?

Jul 17, 2014

I am trying to understand the effect of calling real business logic from the test class. If I have a test class that call the service method which make update to DB. Is DB really changes through test class?

e.g.

Java Code: @Test
public void testUpdate() {
MyDto myDto = new MyDto(paramters to create new myDto object); //creating new dto object
myService.updateMyData(myDto); //passing new dto object for update (DB record really changed??)
MyDto testDto = myRepo.find(myDto.getKey()); //get record for the corresponding key
assert testDto.getSomeProp() == myDto.getSomeProp(); //what testDto.getSomeProp() return? new value send on myDto or old value from DB?
} mh_sh_highlight_all('java');

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

How To Code MMO Real-time Strategy Social Game In Java

Aug 25, 2014

I want to work on a project related to MMO real-time strategy social game as a college project but i dont have enough knowledge on it starting from 3d object coding to server.

Also i would like to know what are things to be cosidered as its an online game whether the camera angel affect what are problems that might occur and how to outrun it. How to split the server so that in one server depending upon the geo-location some number of users will be connected and will be splitted in different servers depending on the location

I want the actions to be cloud based but the actions of the characters will be coded within the game and in every server maximum of 100 users can connect.

Create several rooms.

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

GUI Builder Yes Or No

Dec 23, 2014

I am doing an online course in Java. Mainly for Windows Applications using Eclipse.In the course that I m doing the Instructor has said that most Java programmers dont bother with GUI builders and tend to hand code everyhing. Is this correct? I ask because my previous language I made good use of a GUI builder due to its ease and speed.

Is this because the code is more stable than the generated code offered by GUI builders in Java or is it just better to have full control and to gain a better understanding on how it all works with the layout managers?

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

Checking Values In A String

Apr 15, 2015

Suppose i have a string which has certain file names, S = "a.png, b.gif, c.xlsx, d.docx, e.xlsx, f.gif";I need to check if the string has more than one .xlsx file names,

View Replies View Related







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