visual basic barcode program Passing Primitive Variables in Java

Encode PDF 417 in Java Passing Primitive Variables

Passing Primitive Variables
Decode PDF417 In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
Generate PDF 417 In Java
Using Barcode generator for Java Control to generate, create PDF417 image in Java applications.
Let s look at what happens when a primitive variable is passed to a method:
Decode PDF417 In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
Bar Code Maker In Java
Using Barcode printer for Java Control to generate, create barcode image in Java applications.
class ReferenceTest { public static void main (String [] args) { int a = 1; ReferenceTest rt = new ReferenceTest(); System.out.println("Before modify() a = " + a); rt.modify(a);
Bar Code Decoder In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
Painting PDF 417 In Visual C#
Using Barcode creator for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications.
3: Operators and Assignments
PDF-417 2d Barcode Maker In VS .NET
Using Barcode generator for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
PDF417 Encoder In Visual Studio .NET
Using Barcode encoder for .NET framework Control to generate, create PDF417 image in .NET framework applications.
System.out.println("After modify() a = " + a); } void modify(int number) { number = number + 1; System.out.println("number = " + number); } }
Generating PDF-417 2d Barcode In Visual Basic .NET
Using Barcode creator for .NET framework Control to generate, create PDF 417 image in .NET applications.
USS-128 Drawer In Java
Using Barcode printer for Java Control to generate, create GTIN - 128 image in Java applications.
In this simple program, the variable a is passed to a method called modify(), which increments the variable by 1. The resulting output looks like this:
USS-128 Maker In Java
Using Barcode creator for Java Control to generate, create UCC-128 image in Java applications.
UCC - 12 Encoder In Java
Using Barcode printer for Java Control to generate, create UCC - 12 image in Java applications.
C:\Java Projects\Reference>java ReferenceTest Before modify() a = 1 number = 2 After modify() a = 1
Generating MSI Plessey In Java
Using Barcode generation for Java Control to generate, create MSI Plessey image in Java applications.
ECC200 Creator In .NET
Using Barcode creation for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
Notice that a did not change after it was passed to the method. Remember, it was only a copy of a that was passed to the method. When a primitive variable is passed to a method, it is passed by value, which means pass-by-copy-of-the-bits-inthe-variable.
Create Code 39 In None
Using Barcode generator for Office Word Control to generate, create Code 39 Extended image in Word applications.
Print Code39 In None
Using Barcode maker for Online Control to generate, create Code-39 image in Online applications.
FROM THE CLASSROOM
Generating Code-39 In Visual Studio .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Code 3 of 9 image in .NET applications.
Generate EAN-13 Supplement 5 In None
Using Barcode generator for Font Control to generate, create EAN 13 image in Font applications.
The Shadowy World of Variables
Read Bar Code In Visual C#.NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in VS .NET applications.
Drawing EAN 13 In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create EAN / UCC - 13 image in ASP.NET applications.
Just when you think you ve got it all figured out, you see a piece of code with variables not behaving the way you think they should. You might have stumbled into code with a shadowed variable. You can shadow a variable in several ways; we ll look just at the one most likely to trip you up hiding an instance variable by shadowing it with a local variable. Shadowing involves redeclaring a variable that s already been declared somewhere else. The effect of shadowing is to hide the previously declared variable in such a way that it may look as though you re using the hidden variable, but you re actually using the shadowing variable. You might find reasons to shadow a variable intentionally, but typically it happens by accident and causes hard-to-find bugs. On the exam, you can expect to see questions where shadowing plays a role.
Passing Variables into Methods (Exam Objective 5.4)
FROM THE CLASSROOM
You can shadow an instance variable by declaring a local variable of the same name, either directly or as part of an argument as follows:
class Foo { int size = 7; static void changeIt(int size) { size = size + 200; System.out.println("size in changeIt is " + size); } public static void main (String [] args) { Foo f = new Foo(); System.out.println("size = " + size); changeIt(size); System.out.println("size after changeIt is " + size); } }
The preceding code appears to change the size instance variable in the changeIt() method, but because changeIt() has a
%java Foo size = 7 size in changeIt is 207 size after changeIt is 7
parameter named size, the local size variable is modified while the instance variable size is untouched. Running class Foo prints
Things become more interesting when the shadowed variable is an object reference,
class Bar { int barNum = 28; } class Foo { Bar myBar = new Bar(); void changeIt(Bar myBar) { myBar.barNum = 99;
rather than a primitive:
3: Operators and Assignments
FROM THE CLASSROOM
System.out.println("myBar.barNum in changeIt is " + barNum); myBar = new Bar(); myBar.barNum = 420; System.out.println("myBar.barNum in changeIt is now " + barNum); } public static void main (String [] args) { Foo f = new Foo(); System.out.println("f.myBar.barNum is " + f.myBar.barNum); changeIt(f.myBar); System.out.println("myBar.barNum after changeIt is " + f.myBar.barNum); } }
The preceding code prints out this:
f.myBar.barNum is 28 myBar.barNum in changeIt is 99 myBar.barNum in changeIt is now 420 f.myBar.barNum after changeIt is 99
You can see that the shadowing variable (the local parameter myBar in changeIt()) can still affect the myBar instance variable, because the myBar parameter receives a reference to the same Bar object. But when
the local myBar is reassigned a new Bar object, which we then modify by changing its barNum value, Foo s original myBar instance variable is untouched.
CERTIFICATION SUMMARY
If you ve studied this chapter diligently, and thought of nothing else except this chapter for the last 72 hours, you should have a firm grasp on Java operators. You should understand what equality means when tested with the == operator, and you know how primitives and objects behave when passed to a method. Let s review the highlights of what you ve learned in this chapter.
Passing Variables into Methods (Exam Objective 5.4)
To understand what a bit-shift operation is doing, you need to look at the number being shifted in its binary form. The left shift (<<) shifts all bits to the left, filling the right side with zeroes, and the right shift (>>) shifts all bits right, filling in the left side with whatever the sign bit was. The unsigned right shift (>>>) moves all bits to the right, but fills the left side with zeroes, regardless of the original sign bit. Thus, the result of an unsigned right shift is always a positive number. The logical operators (&& and ||) can be used only to evaluate two boolean expressions. The bitwise operators (& and |) can be used on integral numbers to produce a resulting numeric value, or on boolean values to produce a resulting boolean value. The difference between && and & is that the && operator won t bother testing the right operand if the left evaluates to false, because the result of the && expression can never be true. The difference between || and | is that the || operator won t bother testing the right operand if the left evaluates to true, because the result is already known to be true at that point. The == operator can be used to compare values of primitives, but it can also be used to determine whether two reference variables refer to the same object. Although both objects and primitives are passed by value into a method, key differences exist between how they behave once passed. Objects are passed by a copy of the reference value, while primitives are passed by a copy of the variable value. This means that if an object is modified within a method, other code referring to that object will notice the change. Both the caller and called methods have identical copies of reference variables; therefore, they both refer to the exact same object in memory. Be prepared for a lot of exam questions involving the topics from this chapter. Even within questions testing your knowledge of another objective, the code will frequently use operators, assignments, object and primitive passing, etc., so be on your toes for this topic, and take the Self Test seriously.
Copyright © OnBarcode.com . All rights reserved.