If that sounds rather abstract its because the

Info icon This preview shows pages 711–713. Sign up to view the full content.

View Full Document Right Arrow Icon
static feature is determined at compile type. If that sounds rather abstract, it’s because the distinction can apply to lots of different things, including the choice of which method to call, the type of a variable or an expression, or the meaning of an operator. Let’s look at some concrete examples. The compiler is able to work out quite a lot of things about code during compilation, even code as simple as Example 18-1 . Example 18-1. Simple code with various static features var myString = Console.ReadLine(); var modifiedString = myString.Replace("color", "colour"); We’ve used the var keyword here, so we’ve not told the compiler what type these variables have, but it can work that out for us. The Console.ReadLine() method has a return type of string , meaning that myString must be of type string —the variable’s type can never be anything else, and so we say that it has a static type. (And obviously, the same would be true for any variable declared with an explicit type—declaring myString explicitly as a string would have changed nothing.) Likewise, the compiler is 687
Image of page 711

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
able to work out that modifiedString is also a string . Any variable declared with var will have a static type. The compiler determines other aspects of code statically besides the types of variables. For example, there are method calls. The Console.ReadLine() call is straightforward. Console is a class name, so our code has been explicit about where to find the method. Since there’s no scope for ambiguity over which method we mean, this is a static method invocation—we know at compile time exactly which method will be called at runtime. The myString.Replace method is slightly more interesting: myString refers to a variable, not a class, so to understand which method will be invoked, we need to know what type myString is. But as we already saw, in this example, its type is known statically to be string . As it happens, there are two overloads of Replace , one that takes two string arguments and one that takes two char arguments. In this code, we are passing to string literals, so the argument types are also known statically. This means that the compiler can work out which overload we require, and bakes that choice into the com- piler output—once compilation completes, the exact method that Example 18-1 in- vokes is fixed. All the decisions are made at compile time here, and nothing can change the decision at runtime, and this is the nature of the static style. Dynamic features defer decisions until runtime. For example, in a language that sup- ports dynamic method invocation, the business of working out exactly which method to run doesn’t happen until the program gets to the point where it tries to invoke the method. This means that dynamic code doesn’t necessarily do the same thing every time it runs—a particular piece of code might end up invoking different methods from time to time.
Image of page 712
Image of page 713
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern