So many things are involved in your question, and all this is related to statistics.
SQL compiles the execution plan for even Adhoc requests and stores them in the plan cache for reuse if they are considered safe.
select * into test from sys.objects select schema_id,count(*) from test group by schema_id
First ask: each time we try to use different literals, so sql saves the plan if it considers it safe. You can see that the second query scores are the same as litla 4, since SQL saved the plan for 4
--lets clear cache first--not for prod dbcc freeproccache select * from test where schema_id=4
output:

select * from test where schema_id=1
output:

second question:
Passing the local variable as param, let's use the same value 4
--lets pass 4 which we know has 44 rows,estimates are 44 whem we used literals declare @id int set @id=4 select * from test
As you can see in the screenshot below, the use of local variables is estimated to be less than the approximate 29.5 lines that are associated with the statistics.
output:

Thus, statistics are crucial when choosing a query plan (nested loops or performing a scan or search), from the examples, you can see how the ratings are different for each method. Next from the perspective of planning a tablet plan
You may also wonder what happens if I pass many adhoc requests, as SQL generates a new plan for the same request, even if there is a change in space, below are links that will help you
Further readings:
http://www.sqlskills.com/blogs/kimberly/plan-cache-adhoc-workloads-and-clearing-the-single-use-plan-cache-bloat/
http://sqlperformance.com/2012/11/t-sql-queries/ten-common-threats-to-execution-plan-quality