




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive introduction to fundamental java programming concepts, including variables, data types, and operators. It explains the difference between variables and literals, how to declare and initialize variables, and the various data types available in java. The document also covers arithmetic operators, operator precedence, combined assignment operators, and the distinction between = and ==. It further explores widening, narrowing, and casting concepts in java programming.
Typology: Exams
1 / 101
This page cannot be seen from the preview
Don't miss anything!





























































































int x = 7; How to initialize a variable. - ACCURATE ANSWERS✔✔ Initializing a variable. Initializing a variable means specifying an initial value to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.
The computer also needs to have some reference to where it stored the value, so it can find it again. The concept of a variable solves all of our problems here. A variable in Java gives us a way to store values (or other kinds of information) for later use, addressing all of the aforementioned considerations. The amount of memory allocated for a given v What the arithmetic operators are, and how they work at each data type.
operations. This program also uses + to concatenate strings. The arithmetic operations are shown in boldface: public class ArithmeticDemo { public static void main(String[] args) { //a few numbers int i = 37; int j = 42; double x = 27.475; double y = 7.22; System.out.println("Variable values..."); System.out.println(" i = " + i); System.out.println(" j = " + j); System.out.println(" x = " + x); System.out.println(" y = " + y); //adding numbers System.out.println("Adding..."); System.out.println(" i + j = " + (i + j)); System.out.println(" x + y = " + (x + y));
How to use a combined assignment operator - ACCURATE ANSWERS✔✔ Variables and Combined Assignment Operators You'll commonly want to complete a math function and assign the resulting value back to some named object, called a variable. ActionScript makes this easier by letting you combine arithmetic and assignment operators together. Take a look at an assignment operator example: Create a new ActionScript 3.0 project and enter in the following code for the project: // Assignment Operators var myValue:Number = 2; myValue = myValue + 2; trace(myValue); var myOtherValue:Number = 2; myOtherValue += 2; trace(myOtherValue); Run the project. You'll get the following in the Output panel: 4 4 Let's walk through the code and explain how you get this result and what role variables and combined assignment operators play.
Variables You haven't really seen much about the var statement yet, so let's reveal a little bit more about it. You have used it in the past to create named object containers that you have then assigned MovieClip symbols to using the new statement. You can also use var to create variables; in fact, variables is what var stands for. Variables are named objects that can contain variable values. Take a look at the second line of the assignment operators example: var myValue:Number = 2; The var statement is creating a variable called myValue. See that :Number after the variable name? You have to tell ActionScript what type of data your variable can hold, similar to how you did when using the function statement. In this case, you are saying that myValue will contain a number. When you create the variable, it is empty, but when you assign the numeric value 2 to it, you can refer to that value using the name myValue. myValue = myValue + 2; trace(myValue); On the second line above, you are accessing the myValue object and are assigning a new value to it. Notice that you are not usi
byte b=10; short s= b; //byte value is widened to short int i=b; //byte value is widened to int long l=b; //byte value is widened to long float f=b; //byte value is widened to float double d=b; //byte value is widened to double System.out.println("short value : "+ s); System.out.println("int value : "+ i); System.out.println("long value : "+ l); System.out.println("float value : "+ f); System.out.println("double value : "+ d); } } Output- short value : 10 int value : 10
long value : 10 float value : 10. double value : 10. In the preceding code, we have widened a smaller byte value to several bigger primitive values like byte, short, int, long and float. Widening a subclass object reference to a wider superclass object reference. This is also known as upcasting the subclass reference to its superclass reference. class A { public void message() { System.out.println("message from A"); } } class B extends A
How to cast, and when you would want to do that (and why). - ACCURATE ANSWERS✔✔ How do you cast in Java? In these situations, you can use a process called casting to convert a value from one type to another. Although casting is reasonably simple, the process is complicated by the fact that Java has both primitive types (such as int, float, and boolean) and object types (String, Point, and the like).
How to declare a variable of type String. - ACCURATE ANSWERS✔✔ Ask Question 2 This question already has an answer here: What are classes, references and objects? 9 answers class Demo { String title; private int num; } String is a class, so when we declare title, is that treated as object or just a variable? I know this is a very basic thing, but i need help. Thanks in advance. How to read keyboard input from the user. - ACCURATE ANSWERS✔✔ Input from Keyboard The input Function
Nice to meet you Frank! Your age? 30 So, you are already 30 years old, Frank! It is quite possible that you may have forgotten to include your name into quotes. If so, there is also a good chance that you may have been confused with the error message: Traceback (most recent call last): File "input_test.py", line 1, in
How to use printf to nicely format output - ACCURATE ANSWERS✔✔ A printf format reference page (cheat sheet) By Alvin Alexander. Last updated: December 18 2018 Table of Contents printf formatting with Perl and Java A summary of printf format specifiers Controlling integer width with printf Left-justifying printf integer output The printf integer zero-fill option printf integer formatting formatting floating point numbers with printf printf string formatting printf special characters Related printf content Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here. A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Perl, PHP, Ruby, Scala, and others. This means that your printf knowledge is reusable, which is a good thing.
How to use String.format to nicely format strings. - ACCURATE ANSWERS✔✔ The most common way of formatting a string in java is using String.format(). If there were a "java sprintf" then this would be it. String output = String.format("%s = %d", "joe", 35); For formatted console output, you can use printf() or the format() method of System.out and System.err PrintStreams. Our style conventions for naming program entities, including comments in our programs, and formatting our programs. - ACCURATE ANSWERS✔✔ Google Python Style Guide 1 Background Python is the main dynamic language used at Google. This style guide is a list of dos and don'ts for Python programs. To help you format code correctly, we've created a settings file for Vim. For Emacs, the default settings should be fine. Many teams use the yapf auto-formatter to avoid arguing over formatting. 2 Python Language Rules
2.1 Lint Run pylint over your code. 2.1.1 Definition pylint is a tool for finding bugs and style problems in Python source code. It finds problems that are typically caught by a compiler for less dynamic languages like C and C++. Because of the dynamic nature of Python, some warnings may be incorrect; however, spurious warnings should be fairly infrequent. 2.1.2 Pros Catches easy-to-miss errors like typos, using-vars-before-assignment, etc. 2.1.3 Cons pylint isn't perfect. To take advantage of it, we'll need to sometimes: a) Write around it b) Suppress its warnings or c) Improve it. 2.1.4 Decision Make sure you run pylint on your code.