|
Java's automatic conversion rule for number ⇒ string:
|
public class String05
{
public static void main(String[] args)
{
String a, b;
int x;
a = "abc";
x = -12;
b = a + x;
System.out.println(b); // Prints "abc-12"
b = x + a;
System.out.println(b); // Prints "-12abc"
}
}
|
|
Java's automatic conversion rule for object ⇒ string:
|
public class Class10
{
public static void main(String[] args)
{
String x;
BankAccount stu1 = new BankAccount( 123, "John", 1000 );
x = "Test --- " + stu1 ; // First converts stu1 to a String
System.out.println( x );
}
}
|
Output:
Test --- BankAccount@130c19b |
The string BankAccount@130c19b is the result of the conversion of the object stu1 into the String type
How to run the program:
|
|
|
Furthermore:
|
public class BankAccount
{
private int accNum; // Private access
private String name; // -- limit to ONLY inside
private double balance; // the class "BankAccount"
/* ====================================================
Constructor 1: initialize all 3 instance variables
==================================================== */
public BankAccount(int a, String n, double amount)
{
accNum = a;
name = n;
balance = amount;
}
/* ====================================================
Default Constructor
==================================================== */
public BankAccount( )
{
accNum = -1;
name = "Nobody";
balance = 0.0;
}
/* ==================================================
toString(): return a String containing
information of BankAccount
(BTW, this method used to be called: convToString)
=================================================== */
public String toString( )
{
return( "Account number: " + this.accNum
+ ", Name: " + this.name
+ ", Balance: " + this.balance);
}
/* ==============================================
deposit(amount): Add "amount" to balance
============================================== */
public void deposit( double amount )
{
this.balance += amount;
}
/* ======================================================
withdraw(amount): Subtract "amount" from balance
====================================================== */
public void withdraw( double amount )
{
if ( this.balance >= amount )
this.balance -= amount; // Subtract "amount" from balance
}
}
|
Let's run the same program Class10.java with the new BankAccount class
public class Class10
{
public static void main(String[] args)
{
String x;
BankAccount stu1 = new BankAccount( 123, "John", 1000 );
x = "Test --- " + stu1 ; // Converted to: "Test --- " + stu1.toString()
System.out.println( x );
}
}
|
Output:
Test --- Account number: 123, Name: John, Balance: 1000.0 |
|
|
public class Class11
{
public static void main(String[] args)
{
BankAccount stu1 = new BankAccount( 123, "John", 1000 );
System.out.print( "stu1 = " );
System.out.println( stu1 );
stu1.deposit(450);
System.out.print( "After stu1.deposit(450)... stu1 = " );
System.out.println( stu1 );
BankAccount stu2 = new BankAccount( 456, "Mary", 3000 );
System.out.print( "stu2 = " );
System.out.println( stu2 );
}
}
|
Output:
stu1 = Account number: 123, Name: John, Balance: 1000.0 After stu1.deposit(450)... stu1 = Account number: 123, Name: John, Balance: 1450.0 stu2 = Account number: 456, Name: Mary, Balance: 3000.0 |